C++11库中 steady_clock , system_clock和high_resolution_clock的区别
2023-04-18 14:22:55 时间
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 << "
high_resolution_clock: " << std::endl;
printClockData<std::chrono::high_resolution_clock>();
std::cout << "
steady_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
",#tag,std::chrono::duration_cast<std::chrono::seconds>(tag##_end - tag##_start).count())
#define DURATION_ms(tag) printf("%s costs %d ms
",#tag,std::chrono::duration_cast<std::chrono::milliseconds>(tag##_end - tag##_start).count());
#define DURATION_us(tag) printf("%s costs %d us
",#tag,std::chrono::duration_cast<std::chrono::microseconds>(tag##_end - tag##_start).count());
#define DURATION_ns(tag) printf("%s costs %d ns
",#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
转载于:https://www.cnblogs.com/sunchaothu/p/10551344.html
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击