Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to achieve the CPU frequency on Windows?

30 views
Skip to first unread message

Dongmei Lu

unread,
Jun 30, 2003, 9:45:29 PM6/30/03
to
Hi,
I tried QueryPerformanceFrequency() on my Windows 2000
system with a P4 1.7G CPU. It returns 3579545.

Is there any other way to achieve the CPU frequency on
Windows platform?

Thanks a lot!

Dongmei Lu


David Lowndes

unread,
Jul 1, 2003, 3:01:56 AM7/1/03
to
> I tried QueryPerformanceFrequency() on my Windows 2000
>system with a P4 1.7G CPU. It returns 3579545.
>
> Is there any other way to achieve the CPU frequency on
>Windows platform?

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

Petko Popov

unread,
Jul 10, 2003, 4:57:10 PM7/10/03
to
I believe all tools that measure CPU speed currently use
the RDTSC instruction. Generally, on some notebooks CPU
speed metering fails because the processor seems to freeze
in an idle state whenever it doesn't have what to do in
order to increase battery life. If you keep the machine
busy during the reference period the results will be
correct.

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)

0 new messages