Get the speed of the CPU, more accurate

zhaozj2021-02-12  130

Previously released an article "Rough estimation CPU speed" can get the speed of the CPU, but not very accurate, this time a relatively quasi-program. The principle is exactly the same, just replacing SLEEP with a high-precision counter.

#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; GetCurrentProcess (), priorityClass;

Return (int) ulresult.quadpart / 1000000;}

INT main () {Printf ("CPU Speed ​​=% D MHz / N", Frequence_MHz ());

Author: NowCan Source: http: //nowcan.yeah.net

转载请注明原文地址:https://www.9cbs.com/read-7554.html

New Post(0)