__inline__ uint64_t rdtsc() {
uint32_t low, high;
__asm__ __volatile__ (
"xorl %%eax,%%eax \n cpuid"
::: "%rax", "%rbx", "%rcx", "%rdx" );
__asm__ __volatile__ (
"rdtsc" : "=a" (low), "=d" (high));
return (uint64_t)high << 32 | low;
}
I have used the above rdtsc function as a timer in my program: The following code results in 312-344 clock cycles:
start = rdtsc();
stop = rdtsc();
elapsed_ticks = (unsigned)((stop-start));
printf("\n%u ticks\n",elapsed_ticks);
every time I run the above code I get different values. Why is that?
I ran the same code in Visual C++ which uses an rdtsc function in "intrin.h". I was getting a constant value of 18 clocks.Yes, it was constant on every run! Can someone please explain? Thanks!