zoukankan      html  css  js  c++  java
  • 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
    ", 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
    
    如果您觉得文章不错,不妨给个打赏,写作不易,感谢各位的支持。您的支持是我最大的动力,谢谢!!!




    很重要--转载声明

    1. 本站文章无特别说明,皆为原创,版权所有,转载时请用链接的方式,给出原文出处。同时写上原作者:朝十晚八 or Twowords

    2. 如要转载,请原文转载,如在转载时修改本文,请事先告知,谢绝在转载时通过修改本文达到有利于转载者的目的。


  • 相关阅读:
    LeetCode
    LeetCode
    LeetCode
    LeetCode
    thinkphp使用foreach遍历的方法
    php中foreach中使用&的办法
    thinkphp做搜索功能
    数据库虚拟主机目录的配置文件
    网页响应式设计原理
    数据库常见远程连接问题
  • 原文地址:https://www.cnblogs.com/swarmbees/p/10817838.html
Copyright © 2011-2022 走看看