Skip to content

Instantly share code, notes, and snippets.

@pperon
Last active July 18, 2017 22:10
Show Gist options
  • Save pperon/6e267d9c33480ab1881891ee97e382f3 to your computer and use it in GitHub Desktop.
Save pperon/6e267d9c33480ab1881891ee97e382f3 to your computer and use it in GitHub Desktop.
An example of a high resolution clock.
#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