__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;
}
Run Code Online (Sandbox Code Playgroud)
我在程序中使用了上面的rdtsc函数作为计时器:以下代码导致312-344个时钟周期:
start = rdtsc();
stop = rdtsc();
elapsed_ticks = (unsigned)((stop-start));
printf("\n%u ticks\n",elapsed_ticks);
Run Code Online (Sandbox Code Playgroud)
每次运行上面的代码我都会得到不同的值.这是为什么?
我在Visual C++中运行相同的代码,它在"intrin.h"中使用了rdtsc函数.我得到了18个时钟的恒定值.是的,每次运行都是恒定的!有人可以解释一下吗?谢谢!