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用于计算时间线,如果需要实现的功能与已运行时间有关,则可以使用该部分。
c++时间计算模块的更多相关文章
- python模块:时间处理模块
http://blog.csdn.net/pipisorry/article/details/53067168 常用python自带时间处理模块 python自带的时间处理模块参考[操作系统服务:ti ...
- 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- 【转】Python之日期与时间处理模块(date和datetime)
[转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- 时间处理模块time
一.时间概念 1.1 时间戳 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总 秒数.通俗的讲, 时间戳是一份能够表示一份 ...
- [ Python入门教程 ] Python中日期时间datetime模块使用实例
Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...
- C# 时间计算 今天、昨天、前天、明天 一个月的开始日期与结束日期
C# 时间计算 今天.昨天.前天.明天 class Program { static void Main(string[] args) { ...
- ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间
ac命令根据/var/log/wtmp文件登录退出时间计算用户连接时间
随机推荐
- Oracle 密码失灵
java.sql.SQLException: ORA-28001: 密碼已經屆滿 at oracle.jdbc.driver.DatabaseError.throwSqlException(Datab ...
- MySQL crash-safe replication(2):
MySQL数据库的成功离不开其replicaiton(复制),相对于Oracle DG和Microsoft SQL Server Log Shipping来说,其简单易上手,基本上1,2分钟内根据手册 ...
- JDK5的新特性之 增强for
package cn.itcast.day19.foreach; import java.util.ArrayList; import java.util.Collection; import j ...
- phpstudy绑定项目(dist文件)域名--陈远波
该篇博客是针对已经打包好的dist文件用phpstudy工具进行域名绑定,dist文件生成在这笔者不进行描述,绑定步骤如下: 一:官网下载phpstudy软件进行安装:http://phpstudy. ...
- Secure Shell相关设置
1.清空known hosts记录 ctrl+shift+j调出js控制台后,输入: term_.command.removeAllKnownHosts()
- HTML5音/视频标签详解
一.发展历: 早期:<embed>+<object>+文件 问题:不是所有浏览器都支持,而且embed不是标准. 现状:Realplay.window media.Qu ...
- Tengine 2.1.2 (nginx/1.6.2)安装配置,淘宝 Web 服务器
简介 Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很 ...
- docker 1.13.1 启动容器过程中mount报错
docker 1.13.1 启动container 问题 [root@openfalcon_transfer1 harbor]# docker run -it --name test busybox ...
- Jenkins与Github集成
Jenkins目前是手动进行项目构建的,如何才能做到Github并持续集成呢? 配置前要求: 1.Jenkins已经安装Github插件 2.Jenkins服务器已经拥有一个公网IP地址 第一步:配置 ...
- MUST_COMPLETE
应用: xxx主机: xxx时间: 2018-03-07 04:34:03.887线程: [scheduler-1]级别: ERROR Class: org.springframework.sched ...