从
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()函数如何工作?
解决方法
该函数执行x86指令RTDSC,它的操作码为0x0f,0x31.处理器内部跟踪时钟周期,并读取该数字.
当然,这只适用于x86 procs,其他处理器将需要不同的指令.
The Time Stamp Counter is a 64-bit register present on all x86 processors since the Pentium. It counts the number of ticks since reset. Instruction RDTSC returns the TSC in EDX:EAX. Its opcode is 0F 31.[1] Pentium competitors such as the Cyrix 6×86 did not always have a TSC and may consider RDTSC an illegal instruction. Cyrix included a Time Stamp Counter in their MII.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。