Skip to content

Instantly share code, notes, and snippets.

@zeux
Last active November 26, 2021 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeux/3cd57c16c10699f55cefb88d38be4473 to your computer and use it in GitHub Desktop.
Save zeux/3cd57c16c10699f55cefb88d38be4473 to your computer and use it in GitHub Desktop.
Run with intervals 100, 1000, 10000 as a command line input
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#ifdef __APPLE__
#include <mach/mach_time.h>
#else
struct mach_timebase_info_data_t
{
long numer, denom;
};
void mach_timebase_info(mach_timebase_info_data_t* out)
{
out->numer = out->denom = 1;
}
uint64_t mach_absolute_time()
{
struct timespec ts = {};
clock_gettime(CLOCK_MONOTONIC, &ts);
return long(ts.tv_sec) * long(1e9) + ts.tv_nsec;
}
#endif
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("Usage: %s interval\n", argv[0]);
return 1;
}
int interval = atoi(argv[1]);
mach_timebase_info_data_t timebase = {};
mach_timebase_info(&timebase);
clock_t last_clock = clock();
uint64_t last_time = mach_absolute_time();
for (int i = 0; i < 100; ++i)
{
usleep(interval);
clock_t cur_clock = clock();
uint64_t cur_time = mach_absolute_time();
printf("time delta %.1f us, clock delta %.0f us\n",
double(cur_time - last_time) * double(timebase.numer) / double(timebase.denom) * 1e-3,
double(cur_clock - last_clock) / double(CLOCKS_PER_SEC) * 1e6);
last_clock = cur_clock;
last_time = cur_time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment