VC中监测程序运行时间(二)-毫秒级

时间:2022-07-07 05:58:50
 /*
* 微秒级计时器,用来统计程序运行时间
* http://blog.csdn.net/hoya5121/article/details/3778487#comments
* //整理 [10/16/2013 Duan Yihao]
*/ #pragma once #include "stdafx.h" //////////////////////////////////////////////////////////////////////////
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);
}; //////////////////////////////////////////////////////////////////////////
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 = ;
LARGE_INTEGER i64TickCount;
i64TickCount.QuadPart = m_i64End.QuadPart - m_i64Begin.QuadPart;
dwTotalMicrosecond = (DWORD) (i64TickCount.QuadPart * / m_i64CPUFreq.QuadPart); return dwTotalMicrosecond;
} void timer::reset(void)
{
m_i64Begin = m_i64End;
} /*
//////////////////////////////////////////////////////////////////////////
//MFC下使用举例: void CtestMFCView::OnTest()
{ // TODO: 在此添加命令处理程序代码
//测试例子 //计时开始
timer t;
t.start(); //运算过程 for(int i = 0; i < 10; i++)
{
int n = i;
for(int i = 0; i < 10000; i++)
{
int n = i;
}
} //计时结束
t.end(); //输出运行时间
CString s;
s.Format(_T("%u ms"), t.getTime());
AfxMessageBox(s);
} */