高精度的计时方法
API用法详见:https://docs.microsoft.com/zh-cn/windows/win32/api/profileapi/
下面为封装好的计时类:
#include <profileapi.h>
class CTimer
{
public:
CTimer() : m_nElapsedTime(0)
{
QueryPerformanceFrequency(&m_nFrequency);
}
~CTimer(){}
public:
void Start()
{
QueryPerformanceCounter(&m_nBeginTime);
}
void Stop()
{
LARGE_INTEGER _nEndTime;
QueryPerformanceCounter(&_nEndTime);
m_nElapsedTime = (_nEndTime.QuadPart - m_nBeginTime.QuadPart) * 1000000 / m_nFrequency.QuadPart;
}
void Restart()
{
m_nElapsedTime= 0;
QueryPerformanceCounter(&m_nBeginTime);
}
//微秒
double GetTime_us()
{
return m_nElapsedTime;
}
//毫秒
double GetTime_ms()
{
return m_nElapsedTime/ 1000.0;
}
//秒
double GetTime_s()
{
return m_nElapsedTime/ 1000000.0;
}
private:
LARGE_INTEGER m_nFrequency;
LARGE_INTEGER m_nBeginTime;
double m_nElapsedTime;
};