#include
#include
int __fastcall Frequence_MHz()
{
LARGE_INTEGER ulFreq, ulTicks, ulValue, ulResult;
__int64 ulEAX_EDX, ulStartCounter;
DWORD PriorityClass, Priority;
PriorityClass = GetPriorityClass(GetCurrentProcess());
Priority = GetThreadPriority(GetCurrentThread());
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
// 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;
SetThreadPriority(GetCurrentThread(), Priority);
SetPriorityClass(GetCurrentProcess(), PriorityClass);
return (int)ulResult.QuadPart / 1000000;
}
int main()
{
printf("CPU speed = %d MHz/n",Frequence_MHz());
}
原作者:NowCan
来 源:http://nowcan.yeah.net