How does this code calculate the number of CPU cycles elapsed?

后端 未结 1 1431
时光说笑
时光说笑 2021-02-14 12:38

Taken from this SO thread, this piece of code calculates the number of CPU cycles elapsed running code between lines //1 and //2.

$ cat         


        
相关标签:
1条回答
  • 2021-02-14 13:14

    The function executes the x86 instruction RTDSC, which happens to have an opcode of 0x0f, 0x31. The processor keeps track of clock cycles internally, and this reads that number.

    Of course, this only works on x86 procs, other processors will need different instructions.

    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 6x86 did not always have a TSC and may consider RDTSC an illegal instruction. Cyrix included a Time Stamp Counter in their MII.

    http://en.wikipedia.org/wiki/Time_Stamp_Counter

    0 讨论(0)
提交回复
热议问题