[原]C++新标准之std::chrono::time_point
概览
time_point定义在<chrono>
文件中,用来表示时间点。
类定义
关键代码摘录如下(格式有调整):
- template<class _Clock, class _Duration = typename _Clock::duration>
- class time_point
- {
- public:
- typedef _Clock clock;
- typedef _Duration duration;
- constexpr time_point() : _MyDur(_Duration::zero()) {}
- constexpr explicit time_point(const _Duration& _Other) : _MyDur(_Other) {}
- template<class _Duration2,
- class = typename enable_if<is_convertible<_Duration2, _Duration>::value,void>::type>
- constexpr time_point(const time_point<_Clock, _Duration2>& _Tp) : _MyDur(_Tp.time_since_epoch()) {}
- constexpr _Duration time_since_epoch() const { return (_MyDur); }
- private:
- _Duration _MyDur; // duration since the epoch
- }
注:time_point要求其_Clock模板参数必须满足Clock的要求。
总结
time_point的实现很简单,使用Duration类型的成员变量存储时间,make sense!
仔细想想,时间点不就是从0时刻开始经过一定时间间隔的某一个时刻吗?
思考
- 0是指的哪一个时刻呢?
- 第一个模板参数
Clock
参数如何使用?
拓展
std::chrono提供的clock有system_clock, steady_clock,high_resolution_clock
system_clock
Class std::chrono::system_clock represents the system-wide real time wall clock.
It may not be monotonic: on most systems, the system time can be adjusted at any moment. It is the only C++ clock that has the ability to map its time points to C-style time, and, therefore, to be displayed.
std::chrono::system_clock meets the requirements of TrivialClock.
The epoch of system_clock is unspecified, but most implementations use Unix Time (i.e., time since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds).
(until C++20)
system_clock measures Unix Time (i.e., time since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds).
(since C++20)
相较于steady_clock,system_clock是不稳定的,可能在两次调用之间,系统时间已经被修改了。
steady_clock
Class std::chrono::steady_clock represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward. This clock is not related to wall clock time (for example, it can be time since last reboot), and is most suitable for measuring intervals.
std::chrono::steady_clock meets the requirements of TrivialClock.
steady_clock正如其名,是稳定的。适合用来测量时间间隔。
high_resolution_clock
Class std::chrono::high_resolution_clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.
std::chrono::high_resolution_clock meets the requirements of TrivialClock.
注:在vs中,high_resolution_clock是steady_clock的typedef。
例子
例1. 休眠10秒钟
- std::this_thread::sleep_for(std::chrono::seconds(10));
- std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(10));
例2. 计时代码
一个简单的计时代码,展示了std::chrono::high_resolution_clock和std::chrono::duration的用法。
- std::vector<double> v(10'000'007, 0.5);
- auto t1 = std::chrono::high_resolution_clock::now();
- double result = std::accumulate(v.begin(), v.end(), 0.0);
- auto t2 = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double, std::milli> ms = t2 - t1;
- std::cout << std::fixed << "std::accumulate result " << result << " took " << ms.count() << " ms\n";
std::chrono::system_clock::time_point
定义:
- struct system_clock
- {
- using rep = long long;
- // use system_lock as _Clock template parameter
- using time_point = chrono::time_point<system_clock>;
- };
参考资料
- vs源码
- cppreference
[原]C++新标准之std::chrono::time_point的更多相关文章
- [原]C++新标准之std::chrono::duration
原 总结 C++11 chrono duration ratio 概览 std::chrono::duration 描述 类定义 duration_cast()分析 预定义的duration 示例代 ...
- [原]C++新标准之std::thread
原 总结 C++11 thread 概览 std::thread 类定义 各个成员函数的简单介绍 例子 更多 参考资料 概览 从C++11开始提供了线程的支持,终于可以方便的编写跨平台的线程代码了. ...
- [原]C++新标准之std::ratio
原 总结 ratio 概览 类定义 预定义ratio 应用 示例代码 参考资料 概览 std::ratio定义在<ratio>文件中,提供了编译期的比例计算功能.为std::chrono ...
- C++11 std::chrono库详解
所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又 ...
- C++11新特性,利用std::chrono精简传统获取系统时间的方法
一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...
- C++ 11新特性:std::future & std::shared_future) (转载)
上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...
- c++11 时间类 std::chrono
概念: chrono库:主要包含了三种类型:时间间隔Duration.时钟Clocks和时间点Time point. Duration:表示一段时间间隔,用来记录时间长度,可以表示几秒钟.几分钟或者几 ...
- c++11 标准库函数 std::move 和 完美转发 std::forward
c++11 标准库函数 std::move 和 完美转发 std::forward #define _CRT_SECURE_NO_WARNINGS #include <iostream> ...
- C++11新标准学习
<深入理解C++11:C++11新特性解析与应用> <华章科技:深入理解C++11:C++11新特性解析与应用>一共8章:第1章从设计思维和应用范畴两个维度对C++11新标准中 ...
随机推荐
- 原生js完成打地鼠小游戏
:这是首页,有简单模式和地狱模式两种模式进行选择 这是选择完模式之后的游戏界面:30秒一局游戏倒计时,每打中一只老鼠加一分,没砸中减一分,没砸不加不减 首先准备几张图片 html代码: <!-- ...
- DevOps元数据管理
元数据是自动化运维的基础,对元数据的管理和查询贯穿整个运维的生命周期.我们从一个元数据的使用场景开始: 双十一抢购火热进行中,某电商后端实例的日志中出现了502错误码,运维平台监测到该异常并发送告警给 ...
- 解决vue-cli3不停请求 /sockjs-node/info?t= 问题
使用cli3会遇到一直报错get不到/sockjs-node/info?t= 的问题: 如果你的项目没有用到 sockjs,那么就找到报错的地方,将其注释掉即可. 路径在/node_modules/s ...
- 深入浅出Python装饰器
1.前言 装饰器是Python的特有的语法,刚接触装饰器的同学可能会觉得装饰器很难理解,装饰器的功能也可以不用装饰器实现,但是装饰器无疑是提高你Python代码质量的利器(尤其是使用在一些具有重复功能 ...
- 一个简单的“将ball个球放到box各盒子中,每个盒子不多于m个,并且满足limit条件的状态”的函数
前段时间,做了一个某游戏的辅助计算工具,其中遇到一个排列组合问题.抽象出来就是 将ball个球放到box各盒子中,每个盒子不多于m个,并且满足limit条件, 请给出所有的这些状态. 随意找了下没有现 ...
- kill -HUP 什么意思?
参考 74.在DNS系统测试时,设named进程号是53,命令 D 通知进程重读配置文件.A kill –USR2 53 B kill –USR1 53 C kill -INT 63 D kill – ...
- LeetCode——221. 最大正方形
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 暴力法 ...
- 京东云携手Mellanox,设计最先进SDN硬件加速功能并开源
京东云携手Mellanox,设计最先进SDN硬件加速功能并开源 最新技术播报 京东云开发者社区 导语新一代 SDN.NFV 和云原生计算技术正在推动应用实例的极限,这些实例可以在虚拟化和容器化的服务 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 处理重复数据
有些 MySQL 数据表中可能存在重复的记录,有些情况允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 防止表中出现重复数据 可以在 MySQL 数据表中设置指定的字段为 PRIMARY K ...
- vue项目 首页开发 part3
da当拖动图标时候,只有上部分可以,下部分无响应 swiper 为根页面引用,其中的css为独立,点击swiper标签可以看见其包裹区域只有部分 那么需要修改 就需要穿透样式 外部 >> ...