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

转载于:https://www.cnblogs.com/swarmbees/p/10817838.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值