Introduction

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.

Share

About the Author

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 but i know that this is not possible. It's
real fun for me and I do small Programms for my own
use.
O.K. enough written..... I need my Time to debug
everything that crosses my way!

Comments and Discussions

I have used your tool to try and evaluate the current frequency state of my processor.

First of all I have tinkered with my registry, so the first method is not reliable if you are not the only administrator in the domain.

Second of all as my first screen shot shows my processor is operating at 4.2GHz, solidly confirmed by the temperature of the processor (I'm using Corsair H100i water cooling and still reaching 85°). This is a solid evidence against the outputs of your application (seen in the second screen shot). I suggest revision of the code and comparing this technique to real output from other free system monitoring software.

Hi,
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.

You would probably want to set the thread's priority to high before using the code because QueryPerformanceCounter() and QueryPerformanceFrequency() are keyed to hardware clock time and are completely independent of thread and scheduling issues. So if the thread gets preempted, the clock keeps running, thus skewing the results. In addition, you might want to take multiple samples over a period of time, and then run a simple linear regression.

"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

Just a little thing shouldn't the title of the dialog read get processor frequency as we all know processor frequency doesn't really relate to its speed. I don't think its good to confuse speed with frequency.

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!

"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox

I would say indeed speed is the measure of the amount of work something - like a CPU, for instance - can achieve in one second. The start looks correct to me. Then the amount of work of a CPU fits IMHO the number of *instructions* per second rather than the number of beats per second.

Frequency just gives the average clock speed. A CPU speed is expressed in instructions per second. So unless your CPU is a RISC (which is certainly not the case of an INTEL CPU, or are you talking about how hyperthreading is achieved...), in which case an instruction equals a clock pulse, one instruction is not equal to one hertz.

To make a log story short, the figure you are retrieving is an instant computation of the average CPU clock speed. It is not the CPU inherent speed but only it's clock speed. So it's indeed a frequency.

If you want to use the function to calculate
the speed you have to use it with a Pentium
Processor. For AMD or any other Processor there
must be made some modifications (to the RdTSC
definition I think).

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.

Y3. RDTSC Instruction May Report the Wrong Time Stamp Counter Value
Problem: The Time Stamp Counter is a 64-bit
counter that is read in two 32-bit chunks. The
counter incorrectly advances and therefore the two
chunks may go out of synchronization causing the
Read Time Stamp Counter (RDTSC) instruction to
report the wrong time stamp counter value
Implication: This erratum may cause software to
see the wrong representation of processor time
and may result in unpredictable software operation.
Workaround: It is possible for BIOS to contain a
workaround forthis erratum.
Status: For the steppings affected, see the
Summary of Changes.

this is a problem I don't thought of during building this function. If this is nagging you, decrease the time of measurement. Simply reduce the time to count the cycles and make the final calculating match your values.
Ooops, was your mail a Kick in my a** for an update?
Anyway..... thank you for your comment!!!