Skip to content

Instantly share code, notes, and snippets.

@kghost
Created January 8, 2016 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kghost/656ed1cb253dca759300 to your computer and use it in GitHub Desktop.
Save kghost/656ed1cb253dca759300 to your computer and use it in GitHub Desktop.
Profile CPU time usage
#include <stdio.h>
#if defined(__i386__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#elif defined(__x86_64__)
static __inline__ unsigned long long rdtsc(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
#endif
int main () {
unsigned long long last = rdtsc();
for (;;) {
unsigned long long cur = rdtsc();
unsigned long long diff = cur - last;
if (diff > (1<<20)) // 1M Cycle
printf("Missing %d cycles\n", diff);
last = cur;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment