利用windows API QueryPerformanceFrequency()和QueryPerformanceCounter()写了一个计算精确时间差的类.
用法:initStart()的时候开始计时,此后每调一次GetTickDelta()返回的是距离上次调用GetTickDelta()所经过的毫秒数。
class MyTick
{
public:
MyTick() {
LARGE_INTEGER litmp;
QueryPerformanceFrequency(&litmp);
m_cpuClickPerSecond = (double)litmp.QuadPart;
m_preTick = -1;
}
void initStart()
{
LARGE_INTEGER litmp;
QueryPerformanceCounter(&litmp);
m_preTick = litmp.QuadPart;// 获得初始值
}
int GetTickDelta()
{
LARGE_INTEGER litmp;
QueryPerformanceCounter(&litmp);
LONGLONG curTick = litmp.QuadPart;// 当前tick
if (m_preTick == -1) {
return -1;
} else {
int res = (int)(1000 * (curTick - m_preTick) / m_cpuClickPerSecond);
m_preTick = curTick;
return res;
}
}
private:
double m_cpuClickPerSecond;
LONGLONG m_preTick;
};