俺 转: Victor Chen
One. High-precision delay, is the foundation of the CPU speed of the WINDOWS has a very high precision, the accuracy is in microseconds, but the frequency of different systems is different, this frequency is related to hardware and operating systems. The frequency of this timer can be obtained by using the API function queryperformancefrequency. The current value of the timer can be obtained by using the API function QueryperFormanceCounter. Depending on the time and the frequency of the timer to be delayed, the number of cycles passing through the time timer to be delayed can be calculated. In the cycle, the Timer value is not stopped with QueryPerFormanceCounter, and the end of the specified cycle will end the cycle, the purpose of high-precision delay is achieved. High-precision delayed procedures, parameters: microseconds:
void DelayUs (__ int64 Us) {LARGE_INTEGER CurrTicks, TicksCount; QueryPerformanceFrequency (& TicksCount); QueryPerformanceCounter (& CurrTicks); TicksCount.QuadPart = TicksCount.QuadPart * Us / 1000000i64; TicksCount.QuadPart = CurrTicks.QuadPart; while (CurrTicks.QuadPart two. The measurement program utilizes the RDTSC assembly instruction to obtain the value of the CPU internal timer, and each time a CPU cycle, this timer adds one. If you have a number of CPUs within a period of time, the CPU operating frequency = cycle / time In order not to let other processes and threads disturb, you must set the highest priority to set the current process and thread to the highest priority. SetPriorityClass (GetCurrentProcess (), realtime_priority_class; setthreadPriority (getCurrentthRead (), thread_priority_time_critical; The source code of the CPU speedrower program, this program calculates the working frequency through the CPU in the period of 1/16 seconds, and the unit MHz: int CPU_Frequency (void) // MHz {LARGE_INTEGER CurrTicks, TicksCount; __int64 iStartCounter, iStopCounter; DWORD dwOldProcessP = GetPriorityClass (GetCurrentProcess ()); DWORD dwOldThreadP = GetThreadPriority (GetCurrentThread ()); SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS); SetThreadPriority ( GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL); QueryPerformanceFrequency (& TicksCount); QueryPerformanceCounter (& CurrTicks); TicksCount.QuadPart / = 16; TicksCount.QuadPart = CurrTicks.QuadPart; asm rdtsc asm mov DWORD PTR iStartCounter, EAX asm mov DWORD PTR (iStartCounter 4 ), EDX while (CurrTicks.QuadPart INT _CPU_FREQ = 0; // Defines a global variable to save CPU frequency (MHz) void cpudelayus (__ int64 US) // utilize the frequency delay of cyclic and CPU, parameters: microsecond {__int64 ICOUNTER, ISTOPCOUNTER; ASM RDTSC ASM MOV DWORD PTR iCounter, EAX asm mov DWORD PTR (iCounter 4), EDX iStopCounter = iCounter Us * _CPU_FREQ; while (iStopCounter-iCounter> 0) {asm rdtsc asm mov DWORD PTR iCounter, EAX asm mov DWORD PTR (iCounter 4), EDX}} void testdelay (void) {_CPU_FREQ = CPU_FREQUENCY (); // Using the CPU frequency initialization timing cpudelayus (1000000); // delay 1 second}