Skip to content

Instantly share code, notes, and snippets.

@tanakamura
Created June 7, 2019 14:15
Show Gist options
  • Save tanakamura/89ec7eea4e6580a8007cdcba53cfa6b4 to your computer and use it in GitHub Desktop.
Save tanakamura/89ec7eea4e6580a8007cdcba53cfa6b4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
double getsec() {
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv.tv_sec + tv.tv_nsec/1e9;
}
int main()
{
int n = 100000;
double t0, t1;
struct timespec tv;
t0 = getsec();
for (int i=0; i<n; i++) {
clock_gettime(CLOCK_MONOTONIC,&tv);
}
t1 = getsec();
printf("%f [usec]\n", ((t1-t0)/n)*1e6);
t0 = getsec();
for (int i=0; i<n; i++) {
struct timeval tv;
gettimeofday(&tv, NULL);
}
t1 = getsec();
printf("%f [usec]\n", ((t1-t0)/n)*1e6);
t0 = getsec();
for (int i=0; i<n; i++) {
clock_gettime(CLOCK_MONOTONIC_COARSE,&tv);
}
t1 = getsec();
printf("%f [usec]\n", ((t1-t0)/n)*1e6);
t0 = getsec();
for (int i=0; i<n; i++) {
asm volatile ("rdtsc" ::: "rax", "rdx" );
}
t1 = getsec();
printf("%f [usec]\n", ((t1-t0)/n)*1e6);
t0 = getsec();
for (int i=0; i<n; i++) {
asm volatile ("rdtscp" ::: "rax", "rcx", "rdx");
}
t1 = getsec();
printf("%f [usec]\n", ((t1-t0)/n)*1e6);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment