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文件登录退出时间计算用户连接时间
随机推荐
- JavaScript获取请求参数
<script type="text/javascript"> //获取请求参数 function paramsMap() { var url = window.loc ...
- WEB 报表导入导出操作
/** * 报表导出 * @param response */ @RequestMapping("/stuExcel") @LogAnno(value="对学生数据进行了 ...
- MySQL基础之 统计函数总结
五种统计函数:count().max().avg().min().max()函数 count()函数 count()函数在进行计算的时候,是分情况进行计算的,主要是一下两种 1.采用count(*)对 ...
- win10 损坏 bios?
自从前几个月升级为win10后,主板莫名奇妙的就出问题了,遇到3块不同型号2块技嘉b75,1块微信ph67 技嘉b75-ds3v 技嘉b75m-d3v 微星ph67s-c43 ms-7673 1.0 ...
- css常见知识点
1.内核区分 希望某一个浏览器能一统江湖 -ms-transform:rotate(7deg); //-ms代表ie内核识别码 -moz-transform:rotate(7deg); //-moz代 ...
- 【9】python关于os模块与os.path的相关操作
---恢复内容开始--- #__author:"吉*佳" #date: 2018/10/20 0020 #function: # os模块知识点 import os # 获取平台名 ...
- facebook api & oauth protocal
http://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-10.5 http://stackoverflow.com/questions/14 ...
- Learn Algorithms With Javascript - 基于 Js 进行算法学习
基于 javascript 学习并实现常用的经典算法,欢迎对算法和数学感兴趣的 Js 开发者参与,一起学习共同进步. 算法实现 排序 插入排序 sort/lib/insertion-sort.js 希 ...
- SGU刷题之路开启
VJ小组:SGU---48h/题 每道做出的题目写成题解,将传送门更新在这里. SGU:101 - 200 SGU - 107 水题 解题报告 SGU - 105 找规律水题 解题报告 SGU - 1 ...
- Netty入门(五)ChanneHandler
本节主要讨论了 Netty 的数据处理组件 ChannelHandler. 一.Channel 生命周期 Channel 有个简单但强大的状态模型,下面是 Channel 的四个状态: Channel ...