Skip to content

Instantly share code, notes, and snippets.

@antoni
Created December 4, 2015 12:49
Show Gist options
  • Save antoni/ce4473b8024d7a22fe8c to your computer and use it in GitHub Desktop.
Save antoni/ce4473b8024d7a22fe8c to your computer and use it in GitHub Desktop.
Timing C code
#include <time.h>
#include <stdio.h>
#include <stdint.h>
inline uint64_t rdtsc() {
uint32_t lo, hi;
__asm__ __volatile__ (
"xorl %%eax, %%eax\n"
"cpuid\n"
"rdtsc\n"
: "=a" (lo), "=d" (hi)
:
: "%ebx", "%ecx");
return (uint64_t)hi << 32 | lo;
}
main()
{
// First solution
unsigned long long x;
unsigned long long y;
x = rdtsc();
printf("%lld\n",x);
y = rdtsc();
printf("%lld\n",y);
printf("it took this long to call printf: %lld\n",y-x);
// Alternative solution
clock_t start = clock(), diff;
ProcessIntenseFunction();
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds", msec/1000, msec%1000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment