Hi all,
Here's an alternative way to calculate the CPU frequency.
Display All
The returned frequency is in Hz. To convert to MHz divide the result by 1000000. To convert to GHz divide by 1000000000.
You'll need at least VC6 to compile this as VC5 doesn't support the RDTSC instruction.
Regards,
David Poon.
Here's an alternative way to calculate the CPU frequency.
Source Code
- unsigned __int64 Cpu::calcCpuSpeed() const
- {
- // Calculates the CPU frequency in Hz.
- // Based on CPU code originally written by Emil Persson (aka Humus).
- LARGE_INTEGER time1, time2, freq;
- unsigned __int32 timeLow, timeHigh;
- unsigned __int64 a, b;
- QueryPerformanceFrequency(&freq);
- QueryPerformanceCounter(&time1);
- __asm
- {
- rdtsc ; read the time stamp counter
- mov timeHigh, edx ; store the upper 32-bits of counter
- mov timeLow, eax ; store the lower 32-bits of counter
- }
- a = unsigned __int64(timeHigh << 32) | timeLow;
- for (volatile int i = 0; i < 1000000; ++i)
- ; // spin-wait...do nothing
- QueryPerformanceCounter(&time2);
- __asm
- {
- rdtsc ; read the time stamp counter
- mov timeHigh, edx ; store the upper 32-bits of counter
- mov timeLow, eax ; store the lower 32-bits of counter
- }
- b = unsigned __int64(timeHigh << 32) | timeLow;
- return (b - a) * (freq.QuadPart / (time2.QuadPart - time1.QuadPart));
- }
The returned frequency is in Hz. To convert to MHz divide the result by 1000000. To convert to GHz divide by 1000000000.
You'll need at least VC6 to compile this as VC5 doesn't support the RDTSC instruction.
Regards,
David Poon.
The post was edited 1 time, last by dpoon ().