首页 > 学院 > 开发设计 > 正文

分享一个性能统计的简单封装类

2019-11-10 20:09:28
字体:
来源:转载
供稿:网友

在查找问题,优化性能时,不免用到GetTickCount来统计函数耗时。

不断的

DWord tick_beg = GetTickCount();

DWORD tick_end = GetTickCount();

DWORD tick_span = tick_end - tick_beg;

着实麻烦

简单地封装了一下。

主要提供的接口是

 AutoTick tick(__FUNCTIONW__);

利用作用域在对象析构时自动计算打印。

/********************************************************************created:	2016/11/11file   :	autotick.hauthor :	tulipwiki   :   purpose:	统计性能耗时            主要由析构计算,            GetCurrentConsume:  获取对象构造到当前的耗时            GetLastConsume:    获取上次计算时间点到当前时间点的耗时 *********************************************************************/#ifndef _AUTO_TICK_H_#define _AUTO_TICK_H_class AutoTick {public:    AutoTick(LPTSTR tip) { tip_ = tip; begin_ = GetTickCount(); last_tick_ = begin_;  }    AutoTick() { AutoTick(L"");}    ~AutoTick() {        end_ = GetTickCount();        DWORD span = end_ - begin_;        TCHAR str[1024] = { 0 };        _itow_s(span, str, 1024, 10);        std::wstring strout(L" span:/t/t");        std::wstring tip = tip_ + strout + str + std::wstring(L"/r/n");        OutputDebugString(tip.c_str());    }    // 获取当前的耗时(当前 - 构造)    DWORD GetCurrentConsume(LPTSTR flag = NULL) {        DWORD tick_curr = GetTickCount() - begin_;        if (flag != NULL) {            TCHAR str[1024] = { 0 };            _itow_s(tick_curr, str, 1024, 10);            std::wstring strout(L"Contrust Consume:/t/t");            std::wstring tip = flag + strout + str + std::wstring(L"/r/n");            OutputDebugString(tip.c_str());        }        return tick_curr;    }    DWORD GetCurrentConsume(int index) {        TCHAR ach[64] = { 0 };        _itot_s(index, ach, 10);        GetCurrentConsume(ach);    }    // 获取距离上次计时的耗时(当前 - 上次)    DWORD GetLastConsume(LPTSTR flag = NULL) {        DWORD curr_tick = GetTickCount();             DWORD tick_span = last_tick_ - curr_tick;        if (flag != NULL) {            TCHAR str[1024] = { 0 };            _itow_s(tick_span, str, 1024, 10);            std::wstring strout(L"Last Consume:/t/t");            std::wstring tip = flag + strout + str + std::wstring(L"/r/n");            OutputDebugString(tip.c_str());        }        last_tick_ = curr_tick;        return tick_span;    }    DWORD GetLastConsume(int index) {        TCHAR ach[64] = { 0 };        _itot_s(index, ach, 10);        return GetLastConsume(ach);    }    DWORD begin_;    DWORD end_;    DWORD last_tick_;    std::wstring tip_;};#endif
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表