Mega Code Archive

 
Categories / Delphi / Hardware
 

How to get CPU clock frequency

Title: How to get CPU clock frequency? Question: This code will return CPU frequency... Answer: // returns CPU clock frequency // intDelayTime: measure time in ms - increase to get a more accuracy return function CPUClock(const intDelayTime: integer = 500): double; var TimerHi, TimerLo: DWORD; PriorityClass, Priority: Integer; begin // saves thread priority for the process PriorityClass := GetPriorityClass(GetCurrentProcess); Priority := GetThreadPriority(GetCurrentThread); // sets priority to REAL-TIME SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS); SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL); // "delay" for priority effect Sleep(10); asm dw 310Fh // rdtsc mov TimerLo, eax mov TimerHi, edx end; // waits for calculations Sleep(intDelayTime); asm dw 310Fh // rdtsc sub eax, TimerLo sbb edx, TimerHi mov TimerLo, eax mov TimerHi, edx end; // restores process priority SetThreadPriority(GetCurrentThread, Priority); SetPriorityClass(GetCurrentProcess, PriorityClass); // sets the result with CPU clock frequency result := TimerLo / (1000.0 * intDelayTime); end;