How to Implement latency in microseconds in MFC

Source: Internet
Author: User

Transferred from Baidu Library:

Delayus

 1 void DelayUs(int uDelay) 2 { 3  4     LARGE_INTEGER litmp; 5     LONGLONG QPart1,QPart2; 6  7     double dfMinus,dfFreq,dfTim; 8      9     /*10         Pointer to a variable that the function sets, in counts per second, to the current performance-counter frequency. 11         If the installed hardware does not support a high-resolution performance counter, 12         the value passed back through this pointer can be zero. 13 14     */15     QueryPerformanceFrequency(&litmp);16 17     dfFreq = (double)litmp.QuadPart;18 19     /*20         Pointer to a variable that the function sets, in counts, to the current performance-counter value. 21     */22     QueryPerformanceCounter(&litmp);23 24     QPart1 = litmp.QuadPart;25     do26     {27            QueryPerformanceCounter(&litmp);28         QPart2 = litmp.QuadPart;29         dfMinus = (double)(QPart2-QPart1);30         dfTim = dfMinus/dfFreq;31      }while(dfTim<0.000001 * uDelay);32 }

If you understand the above functions, you can also use some of the Code in it to test the execution time of many functions.

Of course, this function is still a bit risky. If the timer overflows during a delay period, this value may be inaccurate and needs to be handled with caution.

For example, if (dfminus <0)

Break;

This assumes that the hardware timer is counted in an increase.

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.