Skip to content

Instantly share code, notes, and snippets.

@rcls
Created April 1, 2015 03:04
Show Gist options
  • Save rcls/85381db7be9ab442e0db to your computer and use it in GitHub Desktop.
Save rcls/85381db7be9ab442e0db to your computer and use it in GitHub Desktop.
Check the actual granularity of a clock, specifically CLOCK_MONOTONIC_COARSE.
#include <time.h>
#include <stdio.h>
#define CLOCK CLOCK_MONOTONIC_COARSE
int main() {
struct timespec t;
clock_getres(CLOCK, &t);
fprintf(stderr, "Res: %lu %06lu\n", t.tv_sec, t.tv_nsec);
long last = 0;
long nsecs[10];
for (int i = 0; i < 10; ++i) {
do
clock_gettime(CLOCK, &t);
while (t.tv_nsec == last);
nsecs[i] = t.tv_nsec;
last = t.tv_nsec;
}
for (int i = 0; i < 10; ++i) {
fprintf(stderr, "%9lu\n", nsecs[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment