Click here to Skip to main content
13,293,474 members (60,993 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 7 Jun 2004

Get the Processor Speed in two simple ways

, 12 Jun 2004
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! :-)

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 ;-)) 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 | ;-)

You may also be interested in...


Comments and Discussions

SuggestionCalculations or output are ambiguous? Pin
Member 100992673-Mar-15 3:39
memberMember 100992673-Mar-15 3:39 
Generalwin api Pin
swidro17-Feb-11 8:38
memberswidro17-Feb-11 8:38 
GeneralWorks on AMD Athlon 64 4200+ Pin
Gandi13378-Mar-10 16:12
memberGandi13378-Mar-10 16:12 
GeneralThis + an algorithm could be new way to get random numbers Pin
JestBlaze13-Apr-08 16:22
memberJestBlaze13-Apr-08 16:22 
QuestionWhy waiting that second? Pin
Anonymous29-Jul-04 0:58
sussAnonymous29-Jul-04 0:58 
I've just read your article and this question has come to my mind:
Which is the difference between [(float)cyclesStop-(float)cyclesStart] and [(float)nFreq]?
In my opinion is possible to achieve the frequency calculating:
(float)nFreq / 1000000
without waiting that second.

GeneralAccuracy not guaranteed Pin
DavidCrow17-Jun-04 9:05
memberDavidCrow17-Jun-04 9:05 
GeneralThe code I'm posting here has always worked for me Pin
Olan Patrick Barnes14-Jun-04 10:14
memberOlan Patrick Barnes14-Jun-04 10:14 
GeneralRe: The code I'm posting here has always worked for me Pin
Thomas Latuske14-Jun-04 11:39
memberThomas Latuske14-Jun-04 11:39 
GeneralGet Processor Speed should be Get Processor Frequency Pin
technomanceraus8-Jun-04 20:30
membertechnomanceraus8-Jun-04 20:30 
GeneralRe: Get Processor Speed should be Get Processor Frequency Pin
RichardGrimmer9-Jun-04 5:18
memberRichardGrimmer9-Jun-04 5:18 
GeneralRe: Get Processor Speed should be Get Processor Frequency Pin
Anonymous13-Jun-04 18:21
sussAnonymous13-Jun-04 18:21 
GeneralRe: Get Processor Speed should be Get Processor Frequency Pin
Vince C.14-Jun-04 23:22
memberVince C.14-Jun-04 23:22 
GeneralRDTSC Pin
rfmobile8-Jun-04 16:07
memberrfmobile8-Jun-04 16:07 
GeneralRe: RDTSC Pin
Thomas Latuske13-Jun-04 7:04
memberThomas Latuske13-Jun-04 7:04 
GeneralRegistry method works only on NT Pin
dacris8-Jun-04 10:39
memberdacris8-Jun-04 10:39 
GeneralRe: Registry method works only on NT Pin
Rick York8-Jun-04 11:04
memberRick York8-Jun-04 11:04 
GeneralRe: Registry method works only on NT Pin
Thomas Latuske8-Jun-04 11:43
memberThomas Latuske8-Jun-04 11:43 
GeneralCode will break soon... Pin
DiWa8-Jun-04 9:08
memberDiWa8-Jun-04 9:08 
GeneralRe: Code will break soon... Pin
Thomas Latuske8-Jun-04 10:04
memberThomas Latuske8-Jun-04 10:04 

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

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

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