c++时间计算模块

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

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

源码:

  1. //author: cai bingcheng, 2018
  2. #pragma once
  3. #include <iostream>
  4. #include <chrono>
  5. class GetCostTime
  6. {
  7. private:
  8. std::chrono::steady_clock::time_point m_time_pre;
  9. std::chrono::steady_clock::time_point m_time_now;
  10. std::chrono::steady_clock::time_point m_time_line;
  11. std::chrono::duration<double> m_time_cost;
  12. std::chrono::steady_clock::time_point m_time_run_begin;
  13. std::chrono::steady_clock::time_point m_time_run_end;
  14. std::chrono::duration<double> m_time_run_cost;
  15. public:
  16. GetCostTime();
  17. ~GetCostTime();
  18. //init
  19. void GetCostTimeInit();
  20. //calculate cost time after init, refreshing after using
  21. double CalCostTime();
  22. //calculate cost time without refreshing
  23. double CalTimeLine();
  24. //init
  25. void RunTime();
  26. //calculate cost time after init without refreshing
  27. double GetRunTime();
  28. //sleep for m ms
  29. bool WaitTimeMs(double ms);
  30. };
  31. namespace tim{
  32. extern GetCostTime run_time;
  33. }
  1. //author: cai bingcheng, 2018
  2. #include "GetCostTime.h"
  3. using namespace std;
  4. GetCostTime::GetCostTime()
  5. {
  6. }
  7. GetCostTime::~GetCostTime()
  8. {
  9. // std::cout << "-- GetCostTime Destructor successfully!" << std::endl;
  10. }
  11. void GetCostTime::GetCostTimeInit()
  12. {
  13. m_time_now = std::chrono::steady_clock::now();
  14. }
  15. double GetCostTime::CalCostTime()
  16. {
  17. m_time_pre = m_time_now;
  18. m_time_now = std::chrono::steady_clock::now();
  19. m_time_cost = std::chrono::duration_cast<chrono::duration<double>>(m_time_now - m_time_pre);
  20. return m_time_cost.count();
  21. }
  22. double GetCostTime::CalTimeLine()
  23. {
  24. m_time_line = std::chrono::steady_clock::now();
  25. m_time_cost = std::chrono::duration_cast<chrono::duration<double>>(m_time_line - m_time_now);
  26. return m_time_cost.count();
  27. }
  28. void GetCostTime::RunTime()
  29. {
  30. m_time_run_begin = std::chrono::steady_clock::now();
  31. }
  32. double GetCostTime::GetRunTime()
  33. {
  34. m_time_run_end = std::chrono::steady_clock::now();
  35. m_time_run_cost = std::chrono::duration_cast<chrono::duration<double>>(m_time_run_end - m_time_run_begin);
  36. return m_time_run_cost.count();
  37. }
  38. //do not init while use this function
  39. bool GetCostTime::WaitTimeMs(double ms)
  40. {
  41. GetCostTimeInit();
  42. while(CalTimeLine() * 1000 < ms);
  43. return true;
  44. }
  45. namespace tim{
  46. GetCostTime run_time;
  47. }

例程:

  1. //CalCostTime
  2. GetCostTime time;
  3. //init
  4. time.GetCostTimeInit();
  5. yourFunctions_1();
  6. double cost_time = time.CalCostTime();
  7. yourFunctions_2();
  8. double cost_time = time.CalCostTime();
  1. //GetRunTime
  2. GetCostTime time;
  3. time.RunTime();
  4. yourFunctions();
  5. double cost_time = time.GetRunTime();

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

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

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

  1. //CalTimeLine
  2. GetCostTime time;
  3. time.GetCostTimeInit();
  4. yourFunctions();
  5. double cost_time = time.CalTimeLine();
  6. if(cost_time > time_threshold)
  7. yourTasks();

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

c++时间计算模块的更多相关文章

  1. python模块:时间处理模块

    http://blog.csdn.net/pipisorry/article/details/53067168 常用python自带时间处理模块 python自带的时间处理模块参考[操作系统服务:ti ...

  2. 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))

    Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...

  3. 【转】Python之日期与时间处理模块(date和datetime)

    [转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...

  4. Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))

    Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...

  5. Python 日期时间处理模块学习笔记

    来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...

  6. 时间处理模块time

    一.时间概念 1.1 时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总 秒数.通俗的讲, 时间戳是一份能够表示一份 ...

  7. [ Python入门教程 ] Python中日期时间datetime模块使用实例

    Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...

  8. C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期

    C# 时间计算    今天.昨天.前天.明天   class Program    {        static void Main(string[] args)        {          ...

  9. ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间

    ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间

随机推荐

  1. 一张思维导图纵观MySQL数据安全体系!

    杨奇龙 2017-06-29 09:52:10 786 作者介绍 杨奇龙,前阿里数据库团队资深DBA,主要负责淘宝业务线,经历多次双十一,有海量业务访问DB架构设计经验.目前就职于有赞科技,负责数据库 ...

  2. (笔记)MySQL 之 Metadata Locking 研究(5.5版本)

      MySQL5.5 中引入了 metadata lock. 顾名思义,metadata lock 不是为了保护表中的数据的,而是保护 database objects(元数据)的.包括表结构.sch ...

  3. 动态修改css文件中,具体的class中的个别属性值。

    function setStyleSheetObjCssClassProperty(pStyleSheetObj, pSelectorText, pProperty, pValue) { var pS ...

  4. python3: 文件与IO

    1.读写文本数据 # Write chunks of text data with open('somefile.txt', 'wt') as f: f.write(text1) # Redirect ...

  5. spark的shuffle和原理分析

    概述     Shuffle就是对数据进行重组,由于分布式计算的特性和要求,在实现细节上更加繁琐和复杂.    在MapReduce框架,Shuffle是连接Map和Reduce之间的桥梁,Map阶段 ...

  6. burpsuite联合sqlmap扫描注入点

    其实我们在众测的时候完全可以使用burpsuite联合sqlmap测试目标的注入漏洞.对get和post型注入都支持. 先来记录proxy的log , 记住路径     把proxy拦截关掉 接下来浏 ...

  7. NOIP模拟赛-2018.11.5

    NOIP模拟赛 好像最近每天都会有模拟赛了.今天从高二逃考试跑到高一机房,然而高一也要考试,这回好像没有拒绝的理由了. 今天的模拟赛好像很有技术含量的感觉. T1:xgy断句. 好诡异的题目,首先给出 ...

  8. ES6标准入门之数值的拓展解说

    ES6提供了二进制和八进制数值的新写法,分别用前缀0b(或0B)和0o(或0O)表示. 0b111110111 === 503                    // true 0o767 === ...

  9. 【转】1.2 CDN的基本工作过程

    1.2  CDN的基本工作过程 使用CDN会极大地简化网站的系统维护工作量,网站维护人员只需将网站内容注入CDN的系统,通过CDN部署在各个物理位置的服务器进行全网分发,就可以实现跨运营商.跨地域的用 ...

  10. str操作方法

    s = 'dsj,fhk,je,f' # s1 = s.split(',') # print(s1) s = 'aleX leNb' s1 = s.strip() print(s1) s2 = s[2 ...