Build with:
gcc main.c
#include <time.h>
int64_t Timeus () {
struct timespec t;
clock_gettime (CLOCK_MONOTONIC, &t);
int64_t ticks = t.tv_sec * 1000000000ull + t.tv_nsec;
return ticks / 1000;
}
clock_gettime is the POSIX standard way of measuring time. We use CLOCK_MONOTONIC, which is a clock starting from some arbitrary time in the past, guaranteed to never go backward between calls. We get back a struct timespec, which has the two components of seconds and nanoseconds. We combine the two into a single value in microseconds, which will be more than precise enough for any timing or profiling in a game.
It's important to pay attention to the details when combining these numbers. tv_sec and tv_nsec are both of type long, which in C is at least 32 bits. I've found that, on modern 64-bit Linux, longs are 64 bits. Since we're multipling a 64-bit integer by 1 billion, then storing it as a 64-bit integer, there is a possibliity of overflow. However, on Linux, CLOCK_MONOTONIC is specified as measuring time since the system was booted. So, as long as the user's PC has not been running for more than 292 years, this will not overflow.
clock_gettime does return an int: 0 for success, -1 for failure, with errno set. However, I have never known this function to fail, so I skip the check.