C++ chrono 库中的 steady_clock 和 system_clock
C++11 中提供了一个计时的标准库 <chrono>;
里面有三种时钟 clock: steady_clock, system_clock 和 high_resolution_clock;
区别
- steady_clock 是单调的时钟,相当于教练手中的秒表;只会增长,适合用于记录程序耗时;
- system_clock 是系统的时钟;因为系统的时钟可以修改;甚至可以网络对时; 所以用系统时间计算时间差可能不准。
- high_resolution_clock 是当前系统能够提供的最高精度的时钟;它也是不可以修改的。相当于 steady_clock 的高精度版本。
在 参考链接[2] 中提供了代码,可以查看自己的机器上三种时钟的时间精度。
// copied from http://www.informit.com/articles/article.aspx?p=1881386&seqNum=2;
// Author: Nicolai M. Josuttis
#include <chrono>
#include <iostream>
#include <iomanip>
template <typename C>
void printClockData ()
{
using namespace std;
cout << "- precision: ";
// if time unit is less or equal one millisecond
typedef typename C::period P;// type of time unit
if (ratio_less_equal<P,milli>::value) {
// convert to and print as milliseconds
typedef typename ratio_multiply<P,kilo>::type TT;
cout << fixed << double(TT::num)/TT::den
<< " milliseconds" << endl;
}
else {
// print as seconds
cout << fixed << double(P::num)/P::den << " seconds" << endl;
}
cout << "- is_steady: " << boolalpha << C::is_steady << endl;
}
int main()
{
std::cout << "system_clock: " << std::endl;
printClockData<std::chrono::system_clock>();
std::cout << "\nhigh_resolution_clock: " << std::endl;
printClockData<std::chrono::high_resolution_clock>();
std::cout << "\nsteady_clock: " << std::endl;
printClockData<std::chrono::steady_clock>();
#ifdef _WIN32
system("pause");
#endif
return 0;
}
system_clock:
- precision: 0.000100 milliseconds
- is_steady: false
high_resolution_clock:
- precision: 0.000001 milliseconds
- is_steady: true
steady_clock:
- precision: 0.000001 milliseconds
- is_steady: true
建议
以下是 stackoverflow 上一个大佬给出的建议difference between steady clocl vs system clock
- 尽量不要使用
count()方法 - 尽量不要使用
time_since_epoch()
理由是: 提供了类型安全的机制防止用户进行单位换算的时候出错;但是这两个函数是例外的,起到“紧急出口的作用”,
Such emergencies arise when (for example) the committee neglects to give you all the tools you need to get the job done (such as I/O) for the types, or such as the need to interface with some other timing API via integers
在I/O 或者与其他 通过整数传参数的时间函数接口中使用。
使用方法
例子:一个用来测试代码段运行时间的宏
#include <chrono>
#define TIMERSTART(tag) auto tag##_start = std::chrono::steady_clock::now(),tag##_end = tag##_start
#define TIMEREND(tag) tag##_end = std::chrono::steady_clock::now()
#define DURATION_s(tag) printf("%s costs %d s\n",#tag,std::chrono::duration_cast<std::chrono::seconds>(tag##_end - tag##_start).count())
#define DURATION_ms(tag) printf("%s costs %d ms\n",#tag,std::chrono::duration_cast<std::chrono::milliseconds>(tag##_end - tag##_start).count());
#define DURATION_us(tag) printf("%s costs %d us\n",#tag,std::chrono::duration_cast<std::chrono::microseconds>(tag##_end - tag##_start).count());
#define DURATION_ns(tag) printf("%s costs %d ns\n",#tag,std::chrono::duration_cast<std::chrono::nanoseconds>(tag##_end - tag##_start).count());
// usage:
// TIMERSTART(for_loop);
// for (int i = 0; i < 100000; i++)
// {
// i*i;
// }
// TIMEREND(for_loop);
// DURATION_ms(for_loop);
参考
- https://stackoverflow.com/questions/31552193/difference-between-steady-clock-vs-system-clock
- C++ Stardard Library:A Tutorial and Reference, 2nd Edition
C++ chrono 库中的 steady_clock 和 system_clock的更多相关文章
- C++11——chrono库开发高精度计!我们可能学的不是同一门语言~
一.前言 在我们写程序过程中,有时候需要测试我们的程序语句执行时间的耗时,当前也是有很多的库提供我们去使用,一直没有良好的跨平台的库可以提供出来:而且一般这种代码也是由我们程序员自己调用系统的库来进行 ...
- C++11 std::chrono库详解
所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又 ...
- 【Linux开发】OpenCV在ARM-linux上的移植过程遇到的问题4---共享库中嵌套库带路径【已解决】
[Linux开发]OpenCV在ARM-linux上的移植过程遇到的问题4-共享库中嵌套库带路径[已解决] 标签:[Linux开发] 紧接着上一篇,我居然又尝试了一下编译opencv,主要是因为由于交 ...
- 在Azure上的VM镜像库中找到想要的镜像
Azure上的虚机镜像库中, 有很多的镜像,其中当然也包括了用户自定义上传的镜像. 在Powershell中如果想使用这些镜像的话, 则需要知道其名称 下面这条命令,可以获得所有的镜像信息 $imag ...
- Arduino下LCD1602综合探究(下)——如何减少1602的连线,LiquidCrystal库,LiquidCrystal库中bug的解决方法
一.前言: 上文中,笔者系统的阐述了1602的两种驱动方式,并简单的提到了Arduino的LiquidCrystal库.本文紧接上文,对以下两个问题进行更加深入的探讨:如何能够使1602对Arduin ...
- SharePoint 2013 文档库中PPT转换PDF
通过使用 PowerPoint Automation Services,可以从 PowerPoint 二进制文件格式 (.ppt) 和 PowerPoint Open XML 文件格式 (.pptx) ...
- sql 从一个库中取某个表的数据导入到另一个库中相同结构的表中
sql 2008 从一个库中把 某个表中的数据导入到另一个库中的具有相同结构的表中 use 库1 go insert into 库1.dbo.表1 select * from 库2.dbo.表1 ...
- soapUI使用-DataSource获取oracle库中的参数
soapUI使用-DataSource获取oracle库中的参数 下载mysql和oracle驱动包:http://pan.baidu.com/s/1i3sy1MH 放在Program Files\S ...
- XLL 框架库中的函数
这个框架库,可以让编写 XLL 更加容易.包含了管理 XLOPER/XLOPER12 内存的简单函数,创建临时 XLOPER/XLOEPR12 ,强制调用回调函数 (Excel4,Excel4v,Ex ...
随机推荐
- python第四课——运算符
一.python中的运算符: 什么是运算符? 就是计算机语言中用来参与运算的符号!! 1.算数运算符: 符号:+ - * / %(取余,取模) //(取整) **(开方) 2.比较运算符: 特点:比较 ...
- 1.3 Essential Python Libraries(一些重要的Python库)
1.3 Essential Python Libraries(一些重要的Python库) 如果不了解Python的数据生态,以及本书中即将用到的一些库,这里会做一个简单的介绍: Numpy 这里就不过 ...
- swift的enum模式—对Alamofire入口的解析--数据结构与操作结合的模式
swift的枚举模式是数据结构与操作结合的模式 1.enum本质是一个类型,可以定义变量: 它定义的变量可以用到其它变量用的的任何地方:函数的输入.输出.成员变量.临时变量等: 这个变量还可以带有附加 ...
- 整合Yolov3到游戏引擎
这篇其实是前文 CUDA版Grabcut的实现 的后续,和上文一样,先放视频. (博客园好像不支持视频,gif文件太大,视频链接) 在上文用CUDA实现opencv下的grabcut后,当时问题主要是 ...
- Python基础-画图:matplotlib.pyplot.scatter
转载自博客:https://blog.csdn.net/qiu931110/article/details/68130199 matplotlib.pyplot.scatter 1.scatter函数 ...
- [译]OpenGL像素缓冲区对象
目录概述创建PBO映射PBO例子:Streaming Texture Uploads with PBO例子:Asynchronous Readback with PBO 概述 OpenGL ARB_p ...
- sqoop数据导入命令 (sql---hdfs)
mysql------->hdfs sqoop导入数据工作流程: sqoop提交任务到hadoop------>hadoop启动mapreduce------->mapreduce通 ...
- python 内置常用函数
import os def set(o): return set(o) # =={o} def reverseObject(it): it.reverse() return it def sortOb ...
- kubernetes 比较好的案例-创建tomcat-mysql集群
安装部署一个tomcat+mysql应用 apiVersion: v1 kind: ReplicationController metadata: name: myweb spec: //spec ...
- (一)U盘安装ubuntu18.04.1
我选择安装的ubuntu18.04.1因为我的显卡是Amd RX470 ,Amd官方所兼容的版本是18.04.1, 跳过的坑:用18.04.2 18.04.3 安装amdgpu驱动各种报错 0X:下载 ...