统计程序运行时间 微秒级计时器

class timer { public: timer(); ~timer(); void start(void); void end(void); DWORD getTime(void) const; private: LARGE_INTEGER m_i64CPUFreq; LARGE_INTEGER m_i64Begin; LARGE_INTEGER m_i64End; void reset(void); }; //测试例子 void CTest1Dlg::OnOK() { timer t; t.start(); { for(int i = 0; i < 1000000; i++) { int n = i; } } t.end(); CString s; s.Format("%u", t.getTime()); AfxMessageBox(s); } timer::timer() { QueryPerformanceFrequency(&m_i64CPUFreq); } timer::~timer() { } void timer::start(void) { reset(); QueryPerformanceCounter(&m_i64Begin); } void timer::end(void) { QueryPerformanceCounter(&m_i64End); } DWORD timer::getTime(void) const { DWORD dwTotalMicrosecond = 0; LARGE_INTEGER i64TickCount; i64TickCount.QuadPart = m_i64End.QuadPart - m_i64Begin.QuadPart; dwTotalMicrosecond = (DWORD) (i64TickCount.QuadPart * 1000000 / m_i64CPUFreq.QuadPart); return dwTotalMicrosecond; } void timer::reset(void) { m_i64Begin = m_i64End; }

你可能感兴趣的:(timer,测试,Integer,Class)