Detecting CPU Speed: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
m →‎Working Example Code: fixed a bug where the code did not run when TSC was detected ('jnz detect_end' should have been 'jz detect_end')
Line 68: Line 68:
It is possible to create code which is exceptionally pipeline hostile such as:
It is possible to create code which is exceptionally pipeline hostile such as:


<source lang="asm">
<pre>
xor eax,edx
xor eax,edx
xor edx,eax
xor edx,eax
Line 74: Line 74:
xor edx,eax
xor edx,eax
...
...
</pre>
</source>


A simple xor instruction takes one cycle, and it's guaranteed that the processor cannot pipeline this code as the current instructions operands depend on the results from the last calculation. One can check that, for a small count (tested from 16 to 64), RDTSC will show the instruction count is almost exactly (sometimes off by one) the cycles count. Unfortunately, when making the chain longer you'll start experiencing code cache misses, which will ruin the whole process.
A simple xor instruction takes one cycle, and it's guaranteed that the processor cannot pipeline this code as the current instructions operands depend on the results from the last calculation. One can check that, for a small count (tested from 16 to 64), RDTSC will show the instruction count is almost exactly (sometimes off by one) the cycles count. Unfortunately, when making the chain longer you'll start experiencing code cache misses, which will ruin the whole process.