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文件登录退出时间计算用户连接时间
随机推荐
- Netstat Commands for Linux Network Management
netstat (network statistics) is a command line tool for monitoring network connections both incoming ...
- 转:C#综合揭秘——细说多线程(上)
原文地址:http://www.cnblogs.com/leslies2/archive/2012/02/07/2310495.html 引言 本文主要从线程的基础用法,CLR线程池当中工作者线程与I ...
- 一、JSP九大内置对象 二、JAVAEE三层架构和MVC设计模式 三、Ajax
一.JSP九大内置对象###<1>概念 不需要预先申明和定义,可以直接在jsp代码中直接使用 在JSP转换成Servlet之后,九大对象在Servlet中的service方法中对其进行定义 ...
- Priority Queue
优先队列 集合性质的数据类型离不开插入删除这两操作,主要区别就在于删除的时候删哪个,像栈删最晚插入的,队列删最早插入的,随机队列就随便删,而优先队列删除当前集合里最大(或最小)的元素.优先队列有很多应 ...
- Vuex状态管理详解
什么是Vuex 专门为vue应用程序开发的状态管理模式,采用集中式存储管理应用的所有组件的状态(数据),以相应的规则保证状态以一种可预测的方式发生改变 Vuex的作用(什么样的情况下使用Vuex) 多 ...
- ORA-27125: unable to create shared memory segment的解决方法(转)
ORA-27125: unable to create shared memory segment的解决方法(转) # Kernel sysctl configuration file for Red ...
- Node.js实战(十二)之Stream
Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请求的request 对象就是一个 Stream,还有stdout(标准输出). Node.js,Str ...
- vector,deque,list的区别和使用
vector: 表示一段连续的内存区域,每个元素被顺序存储在这段内存中,对vector的随机访问效率很高,但对非末尾元素的插入和删除则效率非常低. deque: 也表示N段连续的内存区域组成,但与ve ...
- Docker学习5-Services – 服务(未完待续)
扩展应用程序并启用负载平衡, 为此,必须在分布式应用程序的层次结构中提升一级:服务.在分布式应用程序中,应用程序的不同部分称为“服务”.例如,一个视频共享站点,它可能包含用于将应用程序数据存储在数据库 ...
- python 爬虫--同花顺-使用代理
1.http://www.goubanjia.com/ 在上面获取 使用http协议的公网IP和端口 参考:https://blog.csdn.net/qq_23934063/article/det ...