Best practice of using __rdtsc

331 Views Asked by At

I am new to system programing, and I have some doubts about how to use __rdtsc.

Here is a quote from Microsoft Learn:

Generates the rdtsc instruction, which returns the processor time stamp. The processor time stamp records the number of clock cycles since the last reset.

Is it a good practice to use following code to measure the CPU cycles of a given operation/function?

#include <x86intrin.h> 

void func() {
    unsigned long long start, end;
    start = __rdtsc();

    // Function call here    

    end = __rdtsc();
    unsigned long long cycles = end - start; 
}

Between start and end, is it possible that CPU switches to another process so that there are some extra CPU cycles recorded in addition to the intended function call? If so, how to measure it precisely?

1

There are 1 best solutions below

4
Some programmer dude On

Between start and end, is it possible that CPU switches to another process so that there are some extra CPU cycles recorded in addition to the intended function call?

Yes.

It's a monotonic always increasing and global counter.

On a multi-tasking system it's not a reliable or accurate way to do benchmarks of anything that might take more than a few operations.

It's also doesn't give true results because it doesn't wait for the pipeline to clear, so some of the instructions you want to benchmark might not even be finished yet for the second call.


If you want to benchmark a function it's better to find a clock with high enough resolution to fit your needs, and that is bound to your process execution and not global (so not a wall-clock like the one used by the clock call on Windows).