测试代码执行时间的帮助类

一同事写的测试代码执行时间的帮助类,

 

// TimeHlper.h

#pragma once

#ifndef NL
#define NL _T("\n")
#endif

#define DEBUG_YGS

#define IDPTRACE_VAL(fmt, val) TRACE(NL _T(__FUNCTION__) _T(#val) fmt, val) // 输出变量
#define IDPTRACE_INT(val) IDPTRACE_VAL(_T("%d"), val) // 输出INT

//////////////////////////////////////////////////////////////////////
// 代码效率统计 -BEGIN
#define IDPCODEEFFICIE_BEGIN \
CIDPCodeEfficie code##__FUNCTION__(_T(__FUNCTION__))

#define IDPCODEEFFICIE_TRACELINE \
code##__FUNCTION__.TraceFunc(__LINE__, _T(__FUNCTION__))

class CIDPCodeEfficie
{
public:
CIDPCodeEfficie(LPCTSTR lpszFlag = _T(""))
{
m_dBeginTime = GetCurTime();
m_dAnchorTime = m_dBeginTime;
m_strFlag = lpszFlag;
}

~CIDPCodeEfficie()
{
//TRACE(_T("\n[%s%08X][%d]:\tAllUsed\n"), m_strFlag, GetTickCount(), GetUsedTime(GetCurTime(), m_dBeginTime));
TRACE(_T("\n[%08X][%s------>AllUsed ======Time:%d]\n"), GetTickCount(), m_strFlag, GetUsedTime(GetCurTime(), m_dBeginTime));
}

VOID TraceFunc(int nLine = 0, LPCTSTR lpszFlag = _T(""))
{
CString strText;
//strText.Format(_T("Func:%s_Line:%d"), lpszFlag, nLine);
//TraceStep(strText);
double dCurTime = GetCurTime();
TRACE(_T("\n[%08X][%s------>Line:%d======Time:%d]"), GetTickCount(), m_strFlag, nLine, GetUsedTime(GetCurTime(), GetAnchorTime()));
SetAnchorTime(dCurTime);
}

VOID TraceStep(LPCTSTR lpszFlag = _T("")) // 输出上次输出时经过的时间
{
double dCurTime = GetCurTime();
TRACE(_T("\n[%s%08X][%d]:\t%s"), m_strFlag, GetTickCount(), GetUsedTime(GetCurTime(), GetAnchorTime()), lpszFlag);
SetAnchorTime(dCurTime);
}

double GetBeginTime() const { return m_dBeginTime; }
double GetAnchorTime() const { return m_dAnchorTime; }
VOID SetAnchorTime(double dTime) { m_dAnchorTime = dTime; }

DWORD GetUsedTime(double dEnd, double dBegin)
{
LARGE_INTEGER litmp;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq = (double)litmp.QuadPart; // 时种频率
dfMinus = (double)(dEnd-dBegin);
dfTim = dfMinus / dfFreq;// 获得对应的时间值,单位为秒
return DWORD(dfTim * 1000);
}

double GetCurTime()
{
LARGE_INTEGER litmp = {0};
QueryPerformanceCounter(&litmp);
return (double)litmp.QuadPart;
}

private:
double m_dBeginTime;
double m_dAnchorTime;
CString m_strFlag;
};

//////////////////////////////////////////////////////////////////////
// 代码效率统计 -End

// ARTIST DEF

posted @ 2014-02-10 11:49  私念之泪  Views(208)  Comments(0Edit  收藏  举报