Click here to Skip to main content
Click here to Skip to main content

Get the Processor Speed in two simple ways

, 12 Jun 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Get the frequency of the processor either from the registry, or calculate it.

Sample Image - Processor_Speed.gif


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! Smile | :)

Routine to retrieve the speed (frequency) from the registry:

This is plain code to retrieve a registry value as a CString:

CString ProcSpeedRead()
CString sMHz;
char Buffer[_MAX_PATH];
HKEY hKey;

// open the key where the proc speed is hidden:
long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    if(lError != ERROR_SUCCESS)
      {// if the key is not found, tell the user why:
           return "N/A";

        // query the key:
        RegQueryValueEx(hKey, "~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);

    // convert the DWORD to a CString:
    sMHz.Format("%i", dwMHz);

return sMHz;

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 Wink | ;-) ) assembler here:

float CGettheProcessorSpeedDlg::ProcSpeedCalc()
It's the Pentium instruction "ReaD Time Stamp Counter". It measures the
number of clock cycles that have passed since the processor was reset, as a
64-bit number. That's what the <CODE>_emit</CODE> lines do.*/
#define RdTSC __asm _emit 0x0f __asm _emit 0x31

// variables for the clock-cycles:
__int64 cyclesStart = 0, cyclesStop = 0;
// variables for the High-Res Preformance Counter:
unsigned __int64 nCtr = 0, nFreq = 0, nCtrStop = 0;

    // retrieve performance-counter frequency per second:
    if(!QueryPerformanceFrequency((LARGE_INTEGER *) &nFreq)) return 0;

    // retrieve the current value of the performance counter:
    QueryPerformanceCounter((LARGE_INTEGER *) &nCtrStop);

    // add the frequency to the counter-value:
    nCtrStop += nFreq;

        {// retrieve the clock-cycles for the start value:
            mov DWORD PTR cyclesStart, eax
            mov DWORD PTR [cyclesStart + 4], edx

        // retrieve the value of the performance counter
        // until 1 sec has gone by:
             QueryPerformanceCounter((LARGE_INTEGER *) &nCtr);
          }while (nCtr < nCtrStop);

        {// retrieve again the clock-cycles after 1 sec. has gone by:
            mov DWORD PTR cyclesStop, eax
            mov DWORD PTR [cyclesStop + 4], edx

// stop-start is speed in Hz divided by 1,000,000 is speed in MHz
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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Thomas Latuske
Web Developer
Germany Germany
My name is Thomas, I'm born on January the 11th in
1970, right now I'm working in the Quality department
of a big Pipe mill as a Technician.
My hobbies are my girl friend, my car, RC-Planes and
Computers. I begun with VC++ some time ago and now
Programming is like a drug to me (I'm still a
beginner). I want to learn it all in a blink of an
eye Wink | ;-) but i know that this is not possible. It's
real fun for me and I do small Programms for my own
O.K. enough written..... I need my Time to debug
everything that crosses my way! Wink | ;-)

Comments and Discussions

SuggestionCalculations or output are ambiguous? PinmemberMember 100992673-Mar-15 3:39 
Generalwin api Pinmemberswidro17-Feb-11 8:38 
GeneralWorks on AMD Athlon 64 4200+ PinmemberGandi13378-Mar-10 16:12 
GeneralThis + an algorithm could be new way to get random numbers PinmemberJestBlaze13-Apr-08 16:22 
QuestionWhy waiting that second? PinsussAnonymous29-Jul-04 0:58 
GeneralAccuracy not guaranteed PinmemberDavidCrow17-Jun-04 9:05 
GeneralThe code I'm posting here has always worked for me PinmemberOlan Patrick Barnes14-Jun-04 10:14 
GeneralRe: The code I'm posting here has always worked for me PinmemberThomas Latuske14-Jun-04 11:39 
GeneralGet Processor Speed should be Get Processor Frequency Pinmembertechnomanceraus8-Jun-04 20:30 
GeneralRe: Get Processor Speed should be Get Processor Frequency PinmemberRichardGrimmer9-Jun-04 5:18 
technomanceraus wrote:
as we all know processor frequency doesn't really relate to its speed
Why's is that then - the "speed" of the processor is a measure of the number of clock cycles per second, and is measured in Hertz - which means that it is most definately a measure of its frequency to me!!
Isn't it just that the frequency in this case is a measure of how much work a processor can do, and how quickly, so surely speed, frequency and even power (work / time) are all accurate!
Confused | :confused:
"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox

GeneralRe: Get Processor Speed should be Get Processor Frequency PinsussAnonymous13-Jun-04 18:21 
GeneralRe: Get Processor Speed should be Get Processor Frequency PinmemberVince C.14-Jun-04 23:22 
GeneralRDTSC Pinmemberrfmobile8-Jun-04 16:07 
GeneralRe: RDTSC PinmemberThomas Latuske13-Jun-04 7:04 
GeneralRegistry method works only on NT Pinmemberdacris8-Jun-04 10:39 
GeneralRe: Registry method works only on NT PinmemberRick York8-Jun-04 11:04 
GeneralRe: Registry method works only on NT PinmemberThomas Latuske8-Jun-04 11:43 
GeneralCode will break soon... PinmemberDiWa8-Jun-04 9:08 
GeneralRe: Code will break soon... PinmemberThomas Latuske8-Jun-04 10:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 13 Jun 2004
Article Copyright 2004 by Thomas Latuske
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid