Created
January 8, 2016 08:41
-
-
Save kghost/656ed1cb253dca759300 to your computer and use it in GitHub Desktop.
Profile CPU time usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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