Created
April 1, 2015 03:04
-
-
Save rcls/85381db7be9ab442e0db to your computer and use it in GitHub Desktop.
Check the actual granularity of a clock, specifically CLOCK_MONOTONIC_COARSE.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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