[原]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新标准中 ...
随机推荐
- oracle(3)select语句中常用的关键字说明
1.select 查询表中的数据 select * from stu: ---查询stu表所有的数据,*代表所有2.dual ,伪表,要查询的数据不存在任何表中时使用 select sysdate f ...
- Floyd--P1119 灾后重建
题目背景 B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车.换句话说,只有连接着两个重建完成的村庄的公路才能 ...
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- caffe中运行mnist
mnist样本字库嘚图片转换:# coding=utf-8import numpy as npimport struct import matplotlib.pyplot as plt from PI ...
- Win10下 Java环境变量配置
安装java的JDK 下载地址 此电脑->属性->高级设置 "系统变量"新建 变量名:Java_Home 变量值:D:\Program Files\Java ...
- hdu1312题解
这道题从名称来看看不出什么. 所以我们先读一下题干 There is a rectangular room, covered with square tiles. Each tile is color ...
- Jenkins 插件中心国内镜像源发布
以下文章来源于Jenkins,作者LinuxSuRen Jenkins 社区的网络基础设施都是架设在国外的服务器上,而且,并没有在国内有 CDN 或者负载均衡的配置.对所有的 Jenkins 用户而言 ...
- Windows如何设置指定的IP走专线?
很多时候在工作中难免有多重网络环境的情况,为了方便之间的互访,可能会用的VPN等虚拟专线,作为网络管理员,route命令是必会的基础技能. 我们一般连接到专线vpn以后,默认会启用远程网关,这样我们所 ...
- 寒假day12
今天写了一点论文,刷了一些算法题
- Pytorch学习--编程实战:猫和狗二分类
Pytorch学习系列(一)至(四)均摘自<深度学习框架PyTorch入门与实践>陈云 目录: 1.程序的主要功能 2.文件组织架构 3. 关于`__init__.py` 4.数据处理 5 ...