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. 使用动态跟踪技术SystemTap监控MySQL、Oracle性能

    [IT168 技术]本文根据吕海波2018年5月11日在[第九届中国数据库技术大会]上的演讲内容整理而成. 讲师介绍: 吕海波,美创科技研究员,ITPUB管理版版主.出版技术书籍<Oracle内 ...

  2. 转:.NET基础篇——反射的奥妙

    反射是一个程序集发现及运行的过程,通过反射可以得到*.exe或*.dll等程序集内部的信息.使用反射可以看到一个程序集内部的接口.类.方法.字段.属性.特性等等信息.在System.Reflectio ...

  3. Python学习--Selenium模块简单介绍(1)

    简介及运行流程 Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozi ...

  4. Windows 7 任务栏图标消失(变透明,仍然占有地方,但是点击无反应)的解决方法

    解决方案:清理资源管理器缓存(重启资源管理器) 1.打开程序管理器(ctrl+shift+esc) 2.在进程那里找到"explorer.exe",然后按结束进程 3.然后在文件( ...

  5. AWS服务学习

    什么是云计算? 云计算是用户通过Internet云服务平台按需提供计算能力.数据库存储.应用程序和其他IT资源,采用按需支付定价模式 无论您是在运行拥有数百万移动用户的照片共享应用程序,还是要为您的业 ...

  6. TiDB数据库 mydumper与loader导入数据

    从mysql导出数据最好的方法是使用tidb官方的工具mydumper. 导入tidb最好的方法是使用loader工具,大概19.4G每小时的速度. 详细的步骤可以参考官网:https://pingc ...

  7. yaml格式

    yaml中允许表示三种格式,分别为常量值.对象和数组 例如: 其中#作为注释,yaml中只有行注释 基本格式要求: 1.大小写敏感:2.使用缩进代表层级关系: 3.缩进只能使用空格,不能使用tab键, ...

  8. 8.2Solr API使用(Facet查询)

    转载请出自出处:http://eksliang.iteye.com/blog/2165882 一)概述 Facet是solr的高级搜索功能之一,可以给用户提供更友好的搜索体验.在搜索关键字的同时,能够 ...

  9. kubernetes lable 操作

    我们要给已创建的Pod“redis-master”添加一个标签role=master: $ kubectl label pod redis-master role=master 查看该Pod的Labe ...

  10. Kubernetes1.91(K8s)安装部署过程(六)--node节点部署

    hi,everybody,我回来了,之前安装到flannel之后,文章一直没有更新,甚至不少小伙伴都来加qq询问是否继续更新了, 这里说明下原因,我在部署1.91node的时候的确出现了各种各样的问题 ...