Microsecond delay based on (QPC) implementation under Windows

Source: Internet
Author: User

1. Why write down microsecond latency in Windows

In the efficiency of testing memcpy in the previous implementation of the memcpy () function and the process summary, the copy efficiency of the test time is in the microsecond level and requires a microsecond interval count.

QueryPerformanceCounter is available under Windows (Querying for high-performance counters), and QPC is based on hardware counters that obtain high-resolution timestamps.

Reference: Acquiring high-resolution time stamps

Application form:

1 large_integer start, end;2 Large_integer Frequency;3QueryPerformanceFrequency (&Frequency);4 5QueryPerformanceCounter (&start);6 7 //Run time body8  9QueryPerformanceCounter (&end);Ten   One //Conversion Time (US) double (end). Quadpart-start. QuadPart) * 1000000/frequency.quadpart

Above the API query high performance counters, start tick, end tick, convert the corresponding time interval.

2. Implementation of US delay based on QPC
1 //timer.c23#include"Timer.h"4 5 Staticlarge_integer start;6 StaticLarge_integer tick;7 StaticLonglong Secondtick;8 9 DoubleGetmicrosecondtimeinterval (Long LongStarttick,Long LongEndtick,Long LongFrequency)Ten { One     return(Double) (Endtick-starttick) *1000000/Frequency; A } -  - /* the * Function:us Delay initialization - * - * Parameter: None - * + * return value: None - *                 + */ A voidMicroseconddelayinit (void) at { - Large_integer frequence; -QueryPerformanceFrequency (&frequence); -Secondtick =frequence.    QuadPart;  - } -  in /* - * Function:microseconddelay (); to * Achieve microsecond delay + *     - * Parameter: the * N: Delayed US number * *     $ * return value:Panax Notoginseng * No - */ the  + voidMicroseconddelay (intN) A { theQueryPerformanceCounter (&start); +     DoubleEndtick = Secondtick * n/1000000.0+start. QuadPart; -      for(;;) $     { $QueryPerformanceCounter (&tick); -         if(tick. QuadPart >=Endtick) -              Break; the     } -}
1 //Timer.h2 3 #pragmaOnce//compiler guarantees that header files are compiled only once4 5#include <windows.h>6#include <stdio.h>7 8 #ifdef __cplusplus9 extern "C" {Ten #endif  One     DoubleGetmicrosecondtimeinterval (Long LongStarttick,Long LongEndtick,Long LongFrequency); A     voidMicroseconddelayinit (void); -     voidMicroseconddelay (intn); - #ifdef __cplusplus the } - #endif 
3.us time-lapse test
1#include <stdio.h>2#include <Windows.h>3#include"Timer.h"4 5 intMainvoid)6 {7 Large_integer Frequency;8 Large_integer startingtime, endingtime;9 TenQueryPerformanceFrequency (&Frequency); One microseconddelayinit (); A  -QueryPerformanceCounter (&startingtime); -Microseconddelay (Ten); theQueryPerformanceCounter (&endingtime); -  -printf"delay:%lf\n", Getmicrosecondtimeinterval (Startingtime.quadpart, Endingtime.quadpart, Frequency.quadpart)); -System"Pause"); +     return 0; -}

Test situation:

1. Delay can reach the US level, multiple test runs, the individual situation delay will be a discrepancy (less in the case).

Analysis Reason: The code-level impact is small, mainly run under Windows, Windows is not a real-time operating system, after all, Windows operating system time resolution can only reach the MS level.

Delay can be interrupted. The frequency of the CPU will change and the code execution efficiency will be affected.

2. This delay effect is significantly better than sleep's MS-level delay.

4.windows US delay, control error

1. Implement US latency on hardware (this is not practical for the underlying hardware operation)

2. Since Windows provides us with QPC (querying high-performance counter <1us), we are cooperating with our own implementation of the US-level delay.

We get the starttick before the delay, and then get the endtick after the delay, the corresponding time interval is converted. The QueryPerformanceCounter function consumes 2 times and can be ignored almost. By printing we can see the US delay number.

Most operating conditions, the delay function effect 1us internal error. More than 1us delay we can eliminate, guaranteed 1us time error. ( This practice is that we need to use the US-class delay when testing, to ensure that the results of subsequent data in a specific delay effect )

5. Summary

The US extension is often used to test some performance. Windows does not pass the US-level delay function. QPC is based on the query hardware counter to obtain the time interval, can reach the US level.

Microsecond delay based on (QPC) implementation under Windows

Related Article

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.