::GetTickCount誤差18ms,所以不適合用於計算時間很短的程序,尤其是算法分析
#include <windows.h>
// 高精度計時器
class HpTimer
{
public:
HpTimer()
{
QueryPerformanceFrequency(&cpuFrequency);
}
void Start()
{
QueryPerformanceCounter(&start);
}
// 秒, 乘以1000就是毫秒
double Elapse()
{
LARGE_INTEGER end;
QueryPerformanceCounter(&end);
double elaspe;
elaspe = ((double)end.QuadPart - (double)start.QuadPart) / (double)cpuFrequency.QuadPart;
return elaspe;
}
private:
LARGE_INTEGER start;
LARGE_INTEGER cpuFrequency;
};