[原]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新标准中 ...
随机推荐
- 【LeetCode】最小路径和
[问题]给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [,,], [,,], [, ...
- 安装Linux系统Centos6版本
1.下载VMware软件 2.下载Centos6文件 http://archive.kernel.org/centos-vault/6.8/isos/x86_64/CentOS-6.8-x86_64- ...
- Java学习笔记(一) 面向对象---封装
面向对象---封装 封装是面向对象思想的三大特征之一. 理解: 隐藏对象的属性和实现细节,仅对外提供公共访问方式. 优点: 将变化隔离 便于使用 提升代码复用性 提高安全性 封装原则: 将不需要对外提 ...
- RecyclerView+FloatingActionButton应用
一.效果图 二.实现步骤 1.XML布局-添加依赖 <LinearLayout android:id="@+id/layout" android:layout_width=& ...
- Centos 7.4 DNS域名解析
1.安装部署包 yum -y install bind bind-utils bind-chroot 2.启动服务并设置开机自启动 [root@localhost ~]# systemctl star ...
- java基础——既然有了字节流,为什么还要有字符流呢?
不管是文件读写还是网络发送接收,信息的最小存储单元都是字节,那为什么I/O流操作要分字节流操作和字符流操作呢? 字符流是由JVM将字节转换得到的,所以这个过程还是非常耗时的,同样假如我们不知道编码方式 ...
- SQL基础之JDBC、连接池
JDBC JDBC四个核心对象 这几个类都是在java.sql包中 DriverManager(类): 数据库驱动管理类.这个类的作用:1)注册驱动; 2)创建java代码和数据库之间的连接,即获取C ...
- 网格视图GridView
1.常用属性 2.Adapter接口 3.Demo演示 今天观看了GridView的相关视频,并且根据案例,进行了代码的编写和实例 新建GridViewActivity.java继承AppCompat ...
- Chrome在新版MacOS上报错 NET::ERR_CERT_WEAK_KEY 解决方法
现象 原文链接 证书详情: 原因 参考苹果官网给出的提示(https://support.apple.com/en-us/HT210176): RSA秘钥长度必须>=2048,小于这个长度的将不 ...
- React编写组件的局部样式
我们都知道,在Vue的单文件组件中,style标签中编写的样式默认为全局样式,如果我们想编写局部样式, 使用一个scoped关键字就可以. 那么在React中怎么实现呢? (注: 这种方法必须使用类选 ...