在Win9X系统中利用如下函数可取得CPU速度,精确度很高:
//---------------------------------------------------------------------------
int __fastcall Frequence_MHz()
{
LARGE_INTEGER ulFreq, ulTicks, ulValue, ulResult;
__int64 ulEAX_EDX, ulStartCounter;
// it is number of ticks per seconds
QueryPerformanceFrequency(&ulFreq);
// current valueofthe performance counter
QueryPerformanceCounter(&ulTicks);
// calculate one second interval
ulValue.QuadPart = ulTicks.QuadPart + ulFreq.QuadPart;
// read time stamp counter
// this asm instruction load the highorder 32 bit of the register into EDX
// and the lower order 32 bits into EAX
_asm {
rdtsc
mov DWORD PTR ulEAX_EDX, EAX
mov DWORD PTR (ulEAX_EDX + 4), EDX
}
// start no of ticks
ulStartCounter = ulEAX_EDX;
// loop for 1 second
do {
QueryPerformanceCounter(&ulTicks);
} while (ulTicks.QuadPart <= ulValue.QuadPart);
// get the actual no of ticks
_asm {
rdtsc
mov DWORD PTR ulEAX_EDX, EAX
mov DWORD PTR (ulEAX_EDX + 4), EDX
}
// calculate result
ulResult.QuadPart = ulEAX_EDX - ulStartCounter;
return (int)ulResult.QuadPart / 1000000;
}
//---------------------------------------------------------------------------
在Win2000/XP系统中,直接可以从注册表中取出,其键值为:
[HKEY_LOCAL_MACHINE\Hardware\Description\System\CentralProcessor\0]
"~MHz"= 作者: qingqiqiu 时间: 2004-10-25 16:02 标题: 巧妙取得CPU的速度