C++记录精确时间-QueryPerformanceFrequency()

来源:互联网 发布:linux怎么解压tar文件 编辑:程序博客网 时间:2024/05/30 23:41

精确获取时间

QueryPerformanceFrequency() - 基本介绍

类型:Win32API

原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

作用:返回硬件支持的高精度计数器的频率。

返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败。

QueryPerformanceFrequency() - 技术特点

供WIN9X使用的高精度定时器:QueryPerformanceFrequency()和QueryPerformanceCounter(),要求计算机从硬件上支持高精度定时器。需包含windows.h头文件。

函数的原形是:

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

BOOL QueryPerformanceCounter (LARGE_INTEGER *lpCount);

数据类型LARGEINTEGER既可以是一个作为8字节长的整数,也可以是作为两个4字节长的整数的联合结构,其具体用法根据编译器是否支持64位而定。该类型的定义如下:

typeef union _ LARGE_INTEGER

{

struct

{

DWORD LowPart;

LONG HighPart;

};

LONGLONG QuadPart;

} LARGE_INTEGER;

在定时前应该先调用QueryPerformanceFrequency()函数获得机器内部计时器的时钟频率。接着在需要严格计时的事件发生前和发生之后分别调用QueryPerformanceCounter(),利用两次获得的计数之差和时钟频率,就可以计算出事件经历的精确时间。

测试Sleep的精确时间:

#include <stdio.h>

#include <windows.h>

void main()

{

     LARGE_INTEGER nFreq;

     LARGE_INTEGER nBeginTime;

     LARGE_INTEGER nEndTime;

     double time;

 

     QueryPerformanceFrequency(&nFreq);

     QueryPerformanceCounter(&nBeginTime); 

 

     Sleep(1000);

 

     QueryPerformanceCounter(&nEndTime);

     time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;

 

     printf("%f\n",time);

     Sleep(1000);

system("Pause");

}

结果为

0.999982

1.000088

1.000200

单位为妙,乘1000000即为微秒

------------------------------------------------------------------------

一个MyTimer类及使用

//MyTimer.h

[cpp] view plain copy
 print?
  1. <span style="font-family:SimSun;font-size:18px;">#ifndef MYTIMER_H  
  2. #define MYTIMER_H  
  3. #include <windows.h>  
  4.   
  5. class MyTimer  
  6. {  
  7. private:  
  8.     LONGLONG _freq;  
  9.     LARGE_INTEGER _begin;  
  10.     LARGE_INTEGER _end;  
  11.   
  12. public:  
  13.     long costTime;            // 花费的时间(精确到微秒)  
  14.   
  15. public:  
  16.     MyTimer()  
  17.     {  
  18.         LARGE_INTEGER tmp;  
  19.         QueryPerformanceFrequency(&tmp);  
  20.         _freq=tmp.QuadPart;  
  21.         costTime=0;  
  22.     }  
  23.   
  24.     void Start()            // 开始计时  
  25.     {  
  26.         QueryPerformanceCounter(&_begin);  
  27.     }  
  28.   
  29.     void End()                // 结束计时  
  30.     {  
  31.         QueryPerformanceCounter(&_end);  
  32.         costTime=(long)((_end.QuadPart - _begin.QuadPart)*1000000/_freq);  
  33.     }  
  34.   
  35.     void Reset()            // 计时清0  
  36.     {  
  37.         costTime = 0;  
  38.     }  
  39. };  
  40. #endif  
  41. </span>  

//MyTimer.cpp

[cpp] view plain copy
 print?
  1. <span style="font-family:SimSun;font-size:18px;">#include "stdafx.h"  
  2. #include "MyTimer.h"  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int _tmain(int argc, _TCHAR* argv[])  
  7. {  
  8.     int i=10000;  
  9.     MyTimer mt;  
  10.     mt.Start();  
  11.     while((i--)>0);  
  12.     mt.End();  
  13.     cout<<"所用时间为 "<<mt.costTime<<"us"<<endl;  
  14.     return 0;  
  15. }  
  16. </span>  
0 0