Last active
July 18, 2017 22:10
-
-
Save pperon/6e267d9c33480ab1881891ee97e382f3 to your computer and use it in GitHub Desktop.
An example of a high resolution clock.
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 <stdio.h> | |
#include <time.h> | |
#include <errno.h> | |
#include <string.h> | |
int main() | |
{ | |
clockid_t clock; | |
if(clock_getcpuclockid(0, &clock) != 0) { | |
strerror(errno); | |
return 1; | |
} | |
struct timespec time = {0}; | |
if(clock_gettime(clock, &time) != 0) { | |
strerror(errno); | |
return 1; | |
} | |
long then = time.tv_nsec; | |
if(clock_gettime(clock, &time) != 0) { | |
strerror(errno); | |
return 1; | |
} | |
long now = time.tv_nsec; | |
long delta = now - then; | |
printf("delta: %ldns\n", delta); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment