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用于计算时间线,如果需要实现的功能与已运行时间有关,则可以使用该部分。

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. Character Sets: Migrating to utf8mb4 with pt_online_schema_change

    David Berube  | June 12, 2018 |  Posted In: MySQL Modern applications often feature the use of data ...

  2. 安装 Scala

    0. 说明 Scala 安装(Windows)  &  Scala 安装(Linux) 1. Scala 安装(Windows) 1.0 下载 Scala  下载地址 1.1 运行 Scala ...

  3. firefox(火狐)和Chrome(google)浏览器清空缓存操作的方法指引

    摘要说明: 1.系统做升级更新,更新了css和js 2.系统更新有,因为缓存问题,有客户反馈新增功能不能使用 3.所以要清空缓存,提供火狐和谷歌浏览器清空缓存的操作指引 4.附:代码层面的清空缓存方案 ...

  4. fedora27安装谷歌浏览器Chrome

    安装 添加chrome源 #cd /etc/yum.repos.d #wget http://repo.fdzh.org/chrome/google-chrome-mirrors.repo 安装Chr ...

  5. 数值分析 最小二乘 matlab

    1. 已知函数在下列各点的值为   -1 -0.75 -0.5 0 0.25 0.5 0.75   1.00 0.8125 0.75 1.00 1.3125 1.75 2.3125 分别用一次.二次. ...

  6. 抓取js动态生成的数据分析案例

    需求:爬取https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html页面中的 ...

  7. Android学习之——自己搭建Http框架(1)

    一.前言         近期学习http框架.         眼下写的这个框架临时仅仅适用于学习之用,实际用于项目之中还须要不断的优化.         要从server或者网络获取数据.显示到U ...

  8. Git branch -r 无法获取远程分支,ui可以看见分支但是git 命令无法查看解决方案

    zhc@hongchangfirst$ git checkout -b hongchangfirst origin/hongchangfirst 出现: fatal: Cannot update pa ...

  9. opengl渲染管线梳理

    opengl渲染管线梳理 http://www.cnblogs.com/zhanglitong/p/3238989.html 坐标系变换和矩阵 http://www.cppblog.com/guoji ...

  10. 容器内部设置JVM的Heap大小

    容器内部利用脚本来获取容器的CGroup资源限制,并通过设置JVM的Heap大小. Docker1.7开始将容器cgroup信息挂载到容器中,所以应用可以从 /sys/fs/cgroup/memory ...