News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Accurate CPU clock speed procedure

Started by MichaelW, February 19, 2006, 08:35:59 PM

Previous topic - Next topic

skywalker

Quote from: Petroizki on April 06, 2006, 09:36:53 AM
Quote from: PBrennick on April 06, 2006, 08:50:04 AM


Paul
The example rounds the clock speed to integer, I changed it to show 2 decimals.

Do you mean that the example returns 500MHz on a 1GHz comp?

The FREQ_DIVIDE_POWER_OF_2 is used as the dividor to shorten the time used to get the CPU speed. The time used to get the speed is '1/2^FREQ_DIVIDE_POWER_OF_2 seconds'. So by using smaller number you would get longer, and probably more accurate CPU speed timing.

I get numbers from 448.90 - 449.00 Mhz on a 448 Mhz system.

PBrennick

Thanks Andy,
Those are pretty much 'on the money,' others are getting normal results as well so there is probably something wrong with my system.  Why one program works and another does not is driving me crazy.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

skywalker

Quote from: PBrennick on April 06, 2006, 09:25:37 PM
Thanks Andy,
Those are pretty much 'on the money,' others are getting normal results as well so there is probably something wrong with my system.  Why one program works and another does not is driving me crazy.

Paul

I know how you feel. Fixin to post some new findings on my high analyzed crypt code. :-)

PBrennick

Andy,
You know that if we can help, we will.  I hope you have made some progress.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

ecube

some c++ code that very accurate and doesn't freeze your system, maybe someone can give a go at converting them? as I don't know how :\



//Count CPU Cycles
static inline unsigned __int64 cyclecount()
{
unsigned int i, j;
__asm
{
rdtsc
mov i, edx;
mov j, eax;
}
return ((unsigned __int64)i << 32) + (unsigned __int64)j;
}



//Get CPU Speed
void get_cpu(char *szBuffer)
{
const unsigned __int64 ui64StartCycle = cyclecount();
unsigned __int64 speed;

Sleep(1000);
speed = ((cyclecount() - ui64StartCycle) / 1000000);
sprintf(szBuffer, "cpu: %dMHZ", speed);
return;
}


the following one I tested a lot

// asm for cpuspeed() (used for counting cpu cycles)
#pragma warning( disable : 4035 )
inline unsigned __int64 GetCycleCount(void)
{
_asm {
_emit 0x0F;
_emit 0x31;
}
}
#pragma warning( default : 4035 )

// cpu speed function
unsigned __int64 GetCPUSpeed(void)
{
unsigned __int64 startcycle, speed, num, num2;

do {
startcycle = GetCycleCount();
Sleep(1000);
speed = ((GetCycleCount()-startcycle)/1000000);
} while (speed > 1000000);

num = speed % 100;
num2 = 100;
if (num < 80) num2 = 75;
if (num < 71) num2 = 66;
if (num < 55) num2 = 50;
if (num < 38) num2 = 33;
if (num < 30) num2 = 25;
if (num < 10) num2 = 0;
speed = (speed-num)+num2;

return (speed);
}