zoukankan      html  css  js  c++  java
  • c++时间计算模块

    c++时间计算模块

    可用于计算代码运行耗时、计算代码运行时间线(比如处理与运行时间相关函数)。

    该模块从实际项目中产生,使用方式仁者见仁智者见智,设计思想可供参考。

    源码:

    //author: cai bingcheng, 2018
    
    #pragma once
    
    #include <iostream>
    #include <chrono>
    
    class GetCostTime
    {
    private:
        std::chrono::steady_clock::time_point m_time_pre;
        std::chrono::steady_clock::time_point m_time_now;
        std::chrono::steady_clock::time_point m_time_line;
        std::chrono::duration<double> m_time_cost;
        std::chrono::steady_clock::time_point m_time_run_begin;
        std::chrono::steady_clock::time_point m_time_run_end;
        std::chrono::duration<double> m_time_run_cost;
    public:
    
        GetCostTime();
        ~GetCostTime();
        //init
        void GetCostTimeInit();
        //calculate cost time after init, refreshing after using
        double CalCostTime();
        //calculate cost time without refreshing
        double CalTimeLine();
        //init
        void RunTime();
        //calculate cost time after init without refreshing
        double GetRunTime();
        //sleep for m ms
        bool WaitTimeMs(double ms);
    };
    
    namespace tim{
        extern GetCostTime run_time;
    }
    
    
    //author: cai bingcheng, 2018
    
    #include "GetCostTime.h"
    
    using namespace std;
    
    GetCostTime::GetCostTime()
    {
    
    }
    
    GetCostTime::~GetCostTime()
    {
      // std::cout << "-- GetCostTime Destructor successfully!" << std::endl;
    }
    
    void GetCostTime::GetCostTimeInit()
    {
      m_time_now = std::chrono::steady_clock::now();
    }
    
    double GetCostTime::CalCostTime()
    {
      m_time_pre = m_time_now;
      m_time_now = std::chrono::steady_clock::now();
      m_time_cost = std::chrono::duration_cast<chrono::duration<double>>(m_time_now - m_time_pre);
      return m_time_cost.count();
    }
    
    double GetCostTime::CalTimeLine()
    {
      m_time_line = std::chrono::steady_clock::now();
      m_time_cost = std::chrono::duration_cast<chrono::duration<double>>(m_time_line - m_time_now);
      return m_time_cost.count();
    }
    
    void GetCostTime::RunTime()
    {
      m_time_run_begin = std::chrono::steady_clock::now();
    }
    
    double GetCostTime::GetRunTime()
    {
      m_time_run_end = std::chrono::steady_clock::now();
      m_time_run_cost = std::chrono::duration_cast<chrono::duration<double>>(m_time_run_end - m_time_run_begin);
      return m_time_run_cost.count();
    }
    
    //do not init while use this function
    bool GetCostTime::WaitTimeMs(double ms)
    {
      GetCostTimeInit();
      while(CalTimeLine() * 1000 < ms);
      return true;
    }
    
    namespace tim{
      GetCostTime run_time;
    }
    
    

    例程:

    //CalCostTime
    GetCostTime time;    
    //init
    time.GetCostTimeInit();
    yourFunctions_1();
    double cost_time = time.CalCostTime();
    yourFunctions_2();
    double cost_time = time.CalCostTime();
    
    //GetRunTime
    GetCostTime time;
    time.RunTime();
    yourFunctions();
    double cost_time = time.GetRunTime();
    

    上述两种方式有略微不同。

    CalCostTime只需初始化一次,调用之后内部自动开始计时,适用于计算相邻代码块时间。

    GetRunTime每次使用之前都需要初始化,适用于计算不相邻代码块时间。

    //CalTimeLine
    GetCostTime time;
    time.GetCostTimeInit();
    yourFunctions();
    double cost_time = time.CalTimeLine();
    if(cost_time > time_threshold)
        yourTasks();
    

    CalTimeLine用于计算时间线,如果需要实现的功能与已运行时间有关,则可以使用该部分。

    分享自由,尊重著作权
  • 相关阅读:
    mini2440移植uboot 2014.04(四)
    mini2440移植uboot 2014.04(三)
    【这些年】Linux C/C++软件开发用过的工具
    Valgrind的Memcheck快速入门
    《浪潮之巅》读后感
    三层浅析及示例分析
    C语言的代码内存布局详解
    超级立方体小记
    如何和项目经理沟通产品的交付?
    CentOS配置smaba与Windows共享文件
  • 原文地址:https://www.cnblogs.com/jerry323/p/9443948.html
Copyright © 2011-2022 走看看