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


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

    // TimeHlper.h

    #pragma once

    #ifndef NL
    #define NL _T(" ")
    #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(" [%s%08X][%d]: AllUsed "), m_strFlag, GetTickCount(), GetUsedTime(GetCurTime(), m_dBeginTime));
    TRACE(_T(" [%08X][%s------>AllUsed ======Time:%d] "), 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(" [%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(" [%s%08X][%d]: %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

    |=======================================| | 既然选择了远方,便只顾风雨兼程! | |=======================================|
  • 相关阅读:
    LeetCode506-相对名次
    LeetCode496-下一个更大的元素(遍历)
    Redis查询超时问题排查及原因分析
    SQL Server 输出消息
    SQL Server按时间分段统计数据
    C# 数据保存到Excel
    查看SQL Server数据库恢复进度
    输入百度网址地址后面有tn小尾巴解决办法
    SQL Server 查询数据大小
    Sping Boot + Spring Security + Mybaits + Logback +JWT验证 项目开发框架搭建
  • 原文地址:https://www.cnblogs.com/mr-m/p/3542591.html
Copyright © 2020-2023  润新知