I'll show you two ways to retrieve the processor-speed (frequency in MHz). With two simple functions, one to retrieve the frequency from the registry of your Windows operating system, and one to calculate it with the clock cycles and a high resolution counter. If you want to use the function to calculate the speed (frequency), you have to use it with a Pentium instruction set compatible processor (look at the lines below).
rfmobile wrote in a message:
You don't need to change the RDTSC definition for non-Intel processors. The code works as-is on my AMD mobile Athlon. Should work on any Pentium instruction set compatible processor but not for 486 or 386.
I'm not able to verify this, so I would like to hear some feedback.
BTW: Constructive criticism is always welcome!
Routine to retrieve the speed (frequency) from the registry:
This is plain code to retrieve a registry value as a
DWORD BufSize = _MAX_PATH;
DWORD dwMHz = _MAX_PATH;
long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
if(lError != ERROR_SUCCESS)
RegQueryValueEx(hKey, "~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);
Routine to calculate the processor frequency in MHz:
Retrieve the frequency in MHz as a
floating-point number. I use some well documented (at least for me ) assembler here:
#define RdTSC __asm _emit 0x0f __asm _emit 0x31
__int64 cyclesStart = 0, cyclesStop = 0unsigned __int64 nCtr = 0, nFreq = 0, nCtrStop = 0
if(!QueryPerformanceFrequency((LARGE_INTEGER *) &nFreq)) return 0
QueryPerformanceCounter((LARGE_INTEGER *) &nCtrStop)
nCtrStop += nFreq
mov DWORD PTR cyclesStart, eax
mov DWORD PTR [cyclesStart + 4], edx
QueryPerformanceCounter((LARGE_INTEGER *) &nCtr);
}while (nCtr < nCtrStop);
mov DWORD PTR cyclesStop, eax
mov DWORD PTR [cyclesStop + 4], edx
return ((float)cyclesStop-(float)cyclesStart) / 1000000;
- I got the assembler some time ago from an assembler newsgroup
- ...and credits to all programmers out there who share their knowing!