Skip to content

Instantly share code, notes, and snippets.

@martani
Created July 26, 2011 17:56
Show Gist options
  • Save martani/1107371 to your computer and use it in GitHub Desktop.
Save martani/1107371 to your computer and use it in GitHub Desktop.
gettimeofday_benchmark
void gettimeofday_benchmark()
{
int i;
struct timespec tv_start, tv_end;
struct timeval tv_tmp;
int count = 1 * 1000 * 1000 * 50;
clockid_t clockid;
int rv = clock_getcpuclockid(0, &clockid);
if (rv) {
perror("clock_getcpuclockid");
return 1;
}
clock_gettime(clockid, &tv_start);
for(i = 0; i < count; i++)
gettimeofday(&tv_tmp, NULL);
clock_gettime(clockid, &tv_end);
long long diff = (long long)(tv_end.tv_sec - tv_start.tv_sec)*(1*1000*1000*1000);
diff += (tv_end.tv_nsec - tv_start.tv_nsec);
printf("%d cycles in %lld ns = %f ns/cycle\n", count, diff, (double)diff / (double)count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment