当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++性能查看-宏定义输出

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

2019年05月06日  | 移动技术网IT编程  | 我要评论

时尚购物,北京印刷公司,高齐

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

使用方式如下:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网