【发布时间】:2011-04-23 07:45:54
【问题描述】:
取自this SO thread,这段代码计算在//1 和//2 行之间运行代码所经过的CPU 周期数。
$ cat cyc.c
#include<stdio.h>
static __inline__ unsigned long long rdtsc(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
int main() {
unsigned long long cycles = rdtsc(); //1
cycles = rdtsc() - cycles; //2
printf("Time is %d\n", (unsigned)cycles);
return 0;
}
$ gcc cyc.c -o cyc
$ ./cyc
Time is 73
$ ./cyc
Time is 74
$ ./cyc
Time is 63
$ ./cyc
Time is 73
$
rdtsc() 函数是如何工作的?
【问题讨论】:
标签: c cpu cpu-cycles