CPU speed (MHz) and high precision latency (microsecond)

Source: Internet
Author: User

Explanation:

High-Precision latency is the basis of CPU speed measurement. in windows, there is a very high-precision timer with the precision in microseconds. However, the timer frequency varies with different systems, this frequency may be related to both hardware and operating systems. You can use the API function queryperformancefrequency to obtain the timer frequency. You can use the API function queryperformancecounter to obtain the current value of the timer. Based on the time to be delayed and the timer frequency, you can calculate the number of cycles of the time timer to be delayed. In a loop, use queryperformancecounter to read the timer value continuously,
The high-precision delay can be achieved after a specified number of cycles and then the cycle ends. High-Precision latency program, parameter: microsecond 2. speed measuring program uses the rdtsc Assembly command to obtain the value of the CPU Internal timer. After each CPU cycle, this timer will add one. If you count the number of CPU cycles within a period of time, the CPU frequency is equal to the number of cycles/time to prevent other processes and threads from disturbing, you must set the highest priority. The following function sets the highest priority for the current process and thread. Setpriorityclass (getcurrentprocess (), realtime_priority_class)
Setthreadpriority (getcurrentthread (), thread_priority_time_critical) source code of the CPU speed measuring program. This program calculates the operating frequency, in MHz, based on the number of cycles that the CPU passes in 1/16 seconds.

High Precision latency:

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<TicksCount.QuadPart)        QueryPerformanceCounter(&CurrTicks);}

Speed Measurement Procedure:

Int cpu_frequency (void) // MHz {large_integer currticks, tickscount; _ int64 istartcounter, istopcounter; DWORD dwoldprocessp = getpriorityclass (getcurrentprocess ()); DWORD priority = getthreadpriority (getcurrentthread (); setpriorityclass (getcurrentprocess (), priority); setthreadpriority (getcurrentthread (), priority); counter (& 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 <tickscount. quadpart) queryperformancecounter (& currticks); ASM rdtsc ASM mov dword ptr istopcounter, eax ASM mov dword ptr (istopcounter + 4), EDX setthreadpriority (getcurrentthread (), dwoldthreadp ); setpriorityclass (getcurrentprocess (), dwoldprocessp); Return (INT) (istopcounter-istartcounter)/62500);} // The API function is used for latency, if you know the CPU operating frequency and use the cycle, you can also get a high-precision delay int _ cpu_freq = 0; // define a global variable to save the CPU frequency (MHz) void cpudelayus (_ int64 US) // uses the cycle and CPU frequency delay. 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 (); // cpudelayus (1000000) during CPU frequency initialization ); // delay 1 second}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.