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
rdtscinstruction, 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?