C++性能查看-宏定义输出

之前由于想统计代码中每个模块加载时长,因此写了一个模块加载时长统计类,使用起来也是超级方便,只需要定义一个宏即可

使用方式如下:

1、统计函数性能

void func()
{
    CONSUMING_OUTPUT("className");
}

2、统计函数中某个模块加载时长

void func()
{
    ...
    {
        //funcation code
        CONSUMING_OUTPUT("code");
    }
    ...
}

3、统计类的存活时长

class A()
{
    ...
    
    CONSUMING_OUTPUT("A life time");
}

//性能查看方便类代码如下

#include <time.h>
#include <windows.h>
#include <iostream>

struct PerformanceCheck
{
public:
    PerformanceCheck(const std::wstring & message) :m_Message(message)
    {
        m_Start = clock();
    }
    ~PerformanceCheck()
    {
        m_End = clock();

        wchar_t str[1024];

        wsprintf(str, L"%s:%d\n", m_Message.c_str(), (long)((double)(m_End - m_Start) / (double)(CLOCKS_PER_SEC)* 1000.0));

#ifdef _DEBUG
        OutputDebugString(str);
#else
        RLBase::WriteProgramLogNoMask(str);
#endif // DEBUG
    }

private:
    clock_t m_Start;
    clock_t m_End;
    std::wstring m_Message;
};

#define PerformanceOutput  //是否启用性能输出

#ifdef PerformanceOutput
#define  CONSUMING_OUTPUT(a) PerformanceCheck c(a)
#else
#define  CONSUMING_OUTPUT(a)
#endif

猜你喜欢

转载自www.cnblogs.com/swarmbees/p/10817838.html
今日推荐