Created
December 4, 2015 12:49
-
-
Save antoni/ce4473b8024d7a22fe8c to your computer and use it in GitHub Desktop.
Timing C code
This file contains hidden or 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 <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