高精度计时器,以前用到过,后来见vckbase.com中有个文章还讲了这个东西,就顺着他的方法做了个封装,感谢人家写了那么多字,我就少写点,直接写有用的。
//------------------------------------------------------------------------------ // 文件名:TimeCounter.H // 说 明:根据硬件定时器的频率得到微妙级别的时间差距。 //------------------------------------------------------------------------------ #ifndef TIMECOUNTER_H_ #define TIMECOUNTER_H_ //------------------------------------------- // include files #include <winbase.h> #include <winnt.h> #pragma comment(lib, "kernel32.lib") //------------------------------------------- // definition #define BENCHMARK_MAX_SIZE 20 //------------------------------------------- // class class CTimeCounter { public: CTimeCounter(); ~CTimeCounter(); void InitBenchmark(); void TC_Start(unsigned int usIndex); void TC_End(unsigned int usIndex); LARGE_INTEGER GetTick(unsigned int usIndex); private: LARGE_INTEGER gStarts[BENCHMARK_MAX_SIZE]; LARGE_INTEGER gEnds[BENCHMARK_MAX_SIZE]; LARGE_INTEGER gCounters[BENCHMARK_MAX_SIZE]; LARGE_INTEGER liFreq; protected: void ResetBenchmarkCounters(); void GetClockFrequent(); }; #endif //TIMECOUNTER_H_ //------------------------------------------------------------------------------ // 文件名:TimeCounter.CPP // 说 明:根据硬件定时器的频率得到微妙级别的时间差距。 //------------------------------------------------------------------------------ //------------------------------------------- // include files #include "StdAfx.h" #include "TimeCounter.H" //------------------------------------------- // implementation /* * 函数名:CTimeCounter * 参 数: * 返回值: * 备 注:标准构造函数 */ CTimeCounter::CTimeCounter() { liFreq.QuadPart = 1; } /* * 函数名:~CTimeCounter * 参 数: * 返回值: * 备 注:标准析构函数 */ CTimeCounter::~CTimeCounter() { } /* * 函数名:InitBenchmark * 参 数: * 返回值: * 备 注:初始化基准 */ void CTimeCounter::InitBenchmark() { ResetBenchmarkCounters(); GetClockFrequent(); } /* * 函数名:TC_Start * 参 数:usIndex: 第几个计数的索引 * 返回值: * 备 注:时间计数器开始计数 */ void CTimeCounter::TC_Start(unsigned int usIndex) { ASSERT((usIndex < BENCHMARK_MAX_SIZE) && (usIndex >= 0)); QueryPerformanceCounter(&gStarts[usIndex]); } /* * 函数名:TC_End * 参 数:usIndex: 第几个计数的索引 * 返回值: * 备 注:时间计数器结束计数 */ void CTimeCounter::TC_End(unsigned int usIndex) { ASSERT((usIndex < BENCHMARK_MAX_SIZE) && (usIndex >= 0)); QueryPerformanceCounter(&gEnds[usIndex]); gCounters[usIndex].QuadPart = (gEnds[usIndex].QuadPart - gStarts[usIndex].QuadPart) * 1000 / liFreq.QuadPart; } /* * 函数名:GetTick * 参 数:usIndex: 第几个计数的索引 * 返回值:LARGE_INTEGER: 返回时间计数(us级) * 备 注:得到时间差 */ LARGE_INTEGER CTimeCounter::GetTick(unsigned int usIndex) { ASSERT((usIndex < BENCHMARK_MAX_SIZE) && (usIndex >= 0)); return gCounters[usIndex]; } /* * 函数名:ResetBenchmarkCounters * 参 数: * 返回值: * 备 注:初始化那几个变量里的值 */ void CTimeCounter::ResetBenchmarkCounters() { memset(gStarts, 0, sizeof(LARGE_INTEGER) * BENCHMARK_MAX_SIZE); memset(gEnds, 0, sizeof(LARGE_INTEGER) * BENCHMARK_MAX_SIZE); memset(gCounters, 0, sizeof(LARGE_INTEGER) * BENCHMARK_MAX_SIZE); } /* * 函数名:GetClockFrequent * 参 数: * 返回值: * 备 注:得到时钟频率 */ void CTimeCounter::GetClockFrequent() { LARGE_INTEGER liFreqTmp; UINT b = QueryPerformanceFrequency(&liFreqTmp); liFreq = liFreqTmp; }
用法:
CTimeCounter tc; tc.InitBenchmark(); tc.TC_Start(1); for(int i = 0; i < 1000000; i++) ; tc.TC_End(1); LARGE_INTEGER liTick; liTick = tc.GetTick(1); CString str; str.Format("100万次空循环的耗时为: %dus.", (long)liTick.QuadPart); AfxMessageBox(str);
输出:用了9us。