Thanks a lot!
Dongmei Lu
The only way I know of is via the registry:
HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
Under the processor specific branch there *may* be a "~MHz" DWORD
value - but not on all operating systems.
Alternatively, you might be able to find it using WMI. See the
Win32_Processor class, CurrentClockSpeed.
Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
You can use the RDTSC instruction in your code. Go to
http://www.intel.com/support/processors/procid/24161812.htm
and download the PDF document. There is a sample in the
appendix.
Here's a snippet to get you started
// make sure RDTSC is supported (use CPUID)
DWORD tscLoDword = 0;
DWORD tscHiDword = 0;
DWORD ticks1 = 0, ticks2 = 0;
DWORD period = 0;
short mHz = 0;
int nCurrentPriority = ::GetThreadPriority
(::GetCurrentThread());
::SetThreadPriority(::GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
// Time Stamp Counter
ticks1 = GetTickCount();
// Read CPU time stamp
__asm {
rdtsc ; Read and save TSC immediately
mov tscLoDword, eax ; after a tick
mov tscHiDword, edx
}
// Keep processor very busy, some laptops will return much
lower processor speeds otherwise
do {
ticks2 = GetTickCount() - ticks1;
} while ( ticks2 < 1000 );
// Read CPU time stamp
__asm {
rdtsc ; Read and save TSC immediately
sub eax, tscLoDword ; Calculate TSC delta from
sbb edx, tscHiDword ; beginning to end of interval
mov tscLoDword, eax ; save
mov tscHiDword, edx
}
if ( nCurrentPriority != THREAD_PRIORITY_ERROR_RETURN ) {
::SetThreadPriority(::GetCurrentThread(),
nCurrentPriority);
}
ticks2 *= 1000; // adjust for MHz
// Taken from the Intel site
// Round to the nearest
_asm {
mov eax, tscLoDword ; Calculate TSC delta from
mov edx, tscHiDword ; beginning to end of interval
mov ebx, ticks2;
div ebx
; ax contains measured speed in MHz
mov mHz, ax
}
// round to the nearest value ending with 00, 33, 50, 66
// 666MHz is always listed as 667
// handle low-speed processors (75, 90MHz)