在一些程序中经常要统计一个算法/函数花费的时间,每次都重新写代码太麻烦了,索性自己用C++写了计时类,这个类统计的时间可以精确到微秒级别
例子:
#include
using namespace std;
///
#ifndef __MyTimer_H__
#define __MyTimer_H__
#include
class MyTimer{
private:
int _freq;
LARGE_INTEGER _begin;
LARGE_INTEGER _end;
public:
long costTime; // 花费的时间(精确到微秒)
public:
MyTimer(){
LARGE_INTEGER tmp;
QueryPerformanceFrequency(&tmp);
_freq = tmp.QuadPart;
costTime = 0;
}
void Start(){ // 开始计时
QueryPerformanceCounter(&_begin);
}
void End(){ // 结束计时
QueryPerformanceCounter(&_end);
costTime = (long)((_end.QuadPart - _begin.QuadPart) * 1000000 / _freq);
}
void Reset(){ // 计时清0
costTime = 0;
}
};
#endif
/
int main(){
MyTimer mt;
mt.Start();
int i;
int sum=0;
for(i=0;i<12345678;i++){
sum=sum+i;
}
mt.End();
cout<
return 0;
}