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. 转:把存储过程结果集SELECT INTO到临时表

    把存储过程结果集SELECT INTO到临时表   在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种.   一. SELECT INTO  1. 使用select into会自动生成临 ...

  2. Rarfile解压不了的问题

    最近用python调用rarfile进行解压rar压缩包时,报了如下错误: rarfile.RarCannotExec: Unrar not installed? (rarfile.UNRAR_TOO ...

  3. JavaBeansDataExchange could not instantiate result class

    当ibatis初始化Bean的时候,会调用无参的构造函数,所以如果Bean中有带参的构造函数,一定得多写个无参的构造函数, 否则ibatis会因找不到构造函数而出错,抛出异常如下:JavaBeansD ...

  4. 利用VBA宏解除Excel保护

    复制以下代码并录制宏,运行一次即可. Option Explicit Public Sub AllInternalPasswords() ' Breaks worksheet and workbook ...

  5. 解决:Windows 强制升级为8.1之后 Mysql连接不上, VisualSVN Server无服务

    1.mysql 连不上,只要将mysql重新加为windows服务即可.(我的是mysql-5.6.24-winx64 解压版)    方法:mysqld --install mysql --defa ...

  6. windows系统显示隐藏文件以及显示文件扩展名

    1.XP系统 打开“我的电脑”,“工具”,“文件夹选项” 勾选如下图 2.win7系统 打开“计算机”,“组织”,“文件夹和搜索选项” 勾选如下图 3.win10系统 打开“此电脑”,“查看”,勾选如 ...

  7. kubernetes label 标签使用

    查看node的标签kubectl get node --show-labels 删除标签kubectl label nodes node5 storagenode- 设置标签kubectl label ...

  8. lua-resty-gearman模块

    粘贴一段百度对gearman的解释: Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在调用其它语言的函数的系统. lu ...

  9. OpenCV——KAZE、AKAZE特征检测、匹配与对象查找

      AKAZE是KAZE的加速版 特征点查找和绘制:把surf中的surf改成KAZE或AKAZE即可 #include <opencv2/opencv.hpp> #include < ...

  10. Python之字典的应用

    # 转化为字符串 d = {'key1' : 'value1', 'key2 ': 'value2','key3' : 'value3'} s = str(d) print(s) #python3 结 ...