zoukankan      html  css  js  c++  java
  • MFC 中怎么实现微秒级的延时 沉沉_

    转自百度文库:

    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     do
    26     {
    27            QueryPerformanceCounter(&litmp);
    28         QPart2 = litmp.QuadPart;
    29         dfMinus = (double)(QPart2-QPart1);
    30         dfTim = dfMinus/dfFreq;
    31      }while(dfTim<0.000001 * uDelay);
    32 }

    如果你看懂了上述函数,还可以用它里面的部分代码来测试很多函数的执行时间。

    当然,这个函数还是有点隐患,如果在一个延时期间,定时器溢出,那么这个值就有可能不准,需要你小心处理。

    如:if (dfMinus < 0)

        break;

    这个假设硬件定时器是以增加的方式计数的。

  • 相关阅读:
    2 浮点数
    1 有符号与无符号
    4.变量和不同的赋值方式
    文档格式标准
    常用mysql命令
    一个简单的jsp+servlet实例,实现简单的登录
    容量调度器与公平调度器区别
    CListCtrl用法总结(二)
    CListCtrl 实现排序功能(数字和字母)
    CString 转换成 char *
  • 原文地址:https://www.cnblogs.com/chenchenluo/p/2721018.html
Copyright © 2011-2022 走看看