zoukankan      html  css  js  c++  java
  • 『转』QueryPerformanceCounter

    转自:http://blog.csdn.net/daoyuly/archive/2009/03/01/3947918.aspx

    精确的时间计时,有时候是非常必要的。比如播放多媒体时视频与音频的时间同步,还有在测试代码的性能时,也需要使用到非常精确的时间计时。还有测试硬件的性能时,也需要精确的时间计时。这时就需要使用QueryPerformanceCounter来查询定时器的计数值,如果硬件里有定时器,它就会启动这个定时器,并且不断获取定时器的值,这样的定时器精度,就跟硬件时钟的晶振一样精确的。

    QueryPerformanceCounter            查询性能计数器 
    The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter, if one exists. 
    此函数用于获取精确的性能计数器数值,如果存在. 
    BOOL QueryPerformanceCounter(

        LARGE_INTEGER *lpPerformanceCount  // address of current counter value   当前计数器值的地址 
       ); 
     

    Parameters

    lpPerformanceCount

    Points to a variable that the function sets, in counts, to the current performance-counter value. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero. 
    指针,指向函数设置的一个变量(一般是个引用,译者注), 用来返回性能计数器的值.如果已安装的硬件不支持高精度性能计数器,此参数可以为0(那调用有什么意义,用来查询?).
     

    Return Values

    If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

    不支持,返回非0
    If the installed hardware does not support a high-resolution performance counter, the return value is zero.   

    否则返回0

    下面有个例子:

    WINBASEAPI
    BOOL
    WINAPI
    QueryPerformanceCounter(
        __out LARGE_INTEGER *lpPerformanceCount
        );
     
    WINBASEAPI
    BOOL
    WINAPI
    QueryPerformanceFrequency(
        __out LARGE_INTEGER *lpFrequency
        );
    lpPerformanceCount是返回定时器当前计数值。
    QueryPerformanceFrequency是返回定时器的频率。
     
    调用函数的例子如下:
     
     //精确时钟查询。
      void TestHighTimer(void)
      {
             //
             LARGE_INTEGER nFreq;
             LARGE_INTEGER nLastTime1;
             LARGE_INTEGER nLastTime2;
     
             //获取是否支持精确定时器。
            if (QueryPerformanceFrequency(&nFreq))
             {
                   //
                   const int nBufSize = 256;
                   TCHAR chBuf[nBufSize];
             
                   //显示定时器的频率。
                   wsprintf(chBuf,_T("LastTime=%I64d\r\n"),nFreq);
                   OutputDebugString(chBuf);
     
                   //获取定时器的值。
                  QueryPerformanceCounter(&nLastTime1);
                   wsprintf(chBuf,_T("LastTime=%I64d\r\n"),nLastTime1);
                   OutputDebugString(chBuf);
                   
                   Sleep(0);
     
                   //获取定时器的值。
                  QueryPerformanceCounter(&nLastTime2);
                   wsprintf(chBuf,_T("LastTime=%I64d\r\n"),nLastTime2);
                   OutputDebugString(chBuf);
     
     
                   //计算时间是花费多少秒。
                   float fInterval = nLastTime2.QuadPart - nLastTime1.QuadPart;
                   swprintf(chBuf,nBufSize,_T("花费:%f\r\n"),fInterval/(float)nFreq.QuadPart);
                   OutputDebugString(chBuf);
             }
              
      }

  • 相关阅读:
    js判断网页是否加载完毕 包括图片
    文本框只能输入数字,输入其他自动过滤 几种方法
    pagebean pagetag java 后台代码实现分页 demo 前台标签分页 后台java分页
    nodejs微信公众号快速开发|自定义关键字回复
    Putty使用ssh方式登录服务器
    什么是Q Learning?
    使用python显示当前系统中的所有进程并关闭某一进程
    树莓派3搭建低成本NAS实现文件共享
    Fortran变量的定义和声明新写法
    Fortran中将多个文件进行编译运行的方法
  • 原文地址:https://www.cnblogs.com/abinxm/p/1665008.html
Copyright © 2011-2022 走看看