原 总结 STL 标准库 chrono time_point ratio 

概览

time_point定义在<chrono>文件中,用来表示时间点。

类定义

关键代码摘录如下(格式有调整):

  1. template<class _Clock, class _Duration = typename _Clock::duration> 

  2. class time_point 



  3. public: 

  4. typedef _Clock clock; 

  5. typedef _Duration duration; 


  6. constexpr time_point() : _MyDur(_Duration::zero()) {} 


  7. constexpr explicit time_point(const _Duration& _Other) : _MyDur(_Other) {} 


  8. template<class _Duration2, 

  9. class = typename enable_if<is_convertible<_Duration2, _Duration>::value,void>::type> 

  10. constexpr time_point(const time_point<_Clock, _Duration2>& _Tp) : _MyDur(_Tp.time_since_epoch()) {} 


  11. constexpr _Duration time_since_epoch() const { return (_MyDur); } 


  12. private: 

  13. _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_clocksystem_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_clocksteady_clock的typedef。

例子

例1. 休眠10秒钟

  1. std::this_thread::sleep_for(std::chrono::seconds(10)); 


  2. std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(10)); 

例2. 计时代码

一个简单的计时代码,展示了std::chrono::high_resolution_clockstd::chrono::duration的用法。

  1. std::vector<double> v(10'000'007, 0.5); 

  2. auto t1 = std::chrono::high_resolution_clock::now(); 

  3. double result = std::accumulate(v.begin(), v.end(), 0.0); 

  4. auto t2 = std::chrono::high_resolution_clock::now(); 

  5. std::chrono::duration<double, std::milli> ms = t2 - t1; 

  6. std::cout << std::fixed << "std::accumulate result " << result << " took " << ms.count() << " ms\n"; 

std::chrono::system_clock::time_point定义:

  1. struct system_clock 



  2. using rep = long long; 

  3. // use system_lock as _Clock template parameter 

  4. using time_point = chrono::time_point<system_clock>; 

  5. }; 

参考资料

[原]C++新标准之std::chrono::time_point的更多相关文章

  1. [原]C++新标准之std::chrono::duration

    原 总结 C++11 chrono duration ratio  概览 std::chrono::duration 描述 类定义 duration_cast()分析 预定义的duration 示例代 ...

  2. [原]C++新标准之std::thread

    原 总结 C++11 thread  概览 std::thread 类定义 各个成员函数的简单介绍 例子 更多 参考资料 概览 从C++11开始提供了线程的支持,终于可以方便的编写跨平台的线程代码了. ...

  3. [原]C++新标准之std::ratio

    原 总结 ratio  概览 类定义 预定义ratio 应用 示例代码 参考资料 概览 std::ratio定义在<ratio>文件中,提供了编译期的比例计算功能.为std::chrono ...

  4. C++11 std::chrono库详解

    所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又 ...

  5. C++11新特性,利用std::chrono精简传统获取系统时间的方法

    一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...

  6. C++ 11新特性:std::future & std::shared_future) (转载)

    上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...

  7. c++11 时间类 std::chrono

    概念: chrono库:主要包含了三种类型:时间间隔Duration.时钟Clocks和时间点Time point. Duration:表示一段时间间隔,用来记录时间长度,可以表示几秒钟.几分钟或者几 ...

  8. c++11 标准库函数 std::move 和 完美转发 std::forward

    c++11 标准库函数 std::move 和 完美转发 std::forward #define _CRT_SECURE_NO_WARNINGS #include <iostream> ...

  9. C++11新标准学习

    <深入理解C++11:C++11新特性解析与应用> <华章科技:深入理解C++11:C++11新特性解析与应用>一共8章:第1章从设计思维和应用范畴两个维度对C++11新标准中 ...

随机推荐

  1. 面试官,不要再问我“Java 垃圾收集器”了(转载)

    如果Java虚拟机中标记清除算法.标记整理算法.复制算法.分代算法这些属于GC收集算法中的方法论,那么"GC收集器"则是这些方法论的具体实现. 在 面试过程中这个深度的问题涉及的比 ...

  2. Codeforces 437D 贪心+并查集

    这个题目让我想起了上次在湘潭赛的那道跪死了的题.也是最值问题,这个也是,有n个动物园 每个都有权值 然后被m条路径相连接,保证图是连通的,然后求所有的p[i][j]之和.i,j为任意两个zoo,pij ...

  3. 干货|微软远程桌面服务蠕虫漏洞(CVE-2019-1182)分析

    2019年8月,微软发布了一套针对远程桌面服务的修复程序,其中包括两个关键的远程执行代码(RCE)漏洞,CVE-2019-1181和CVE-2019-1182.与之前修复的"BlueKeep ...

  4. VS2013 MFC opencv 播放视频

    看网上有很多人用的还是CvvImage类,但是Opencv3.0已经没有CvvImage这个类了.百度得之可以使用以前的类,稍作修改就可以了. 头文件: #pragma once #ifndef CV ...

  5. EOJ Monthly 2020.1 E. 数的变幻

    题目链接:https://acm.ecnu.edu.cn/contest/247/problem/E/ 这道题是cf原题: Codeforces Round #608 (Div. 2) E. Comm ...

  6. D语言-运算符

    Part 0:概念 表达式:表达式是由非赋值运算符或特殊运算符和值组成的,每个表达式都可以计算出一个值 Part 1:非赋值运算符 §1.1 基本的运算符 基本的运算符有+,-,*,/,% 我相信你除 ...

  7. 算概率(dp,数论)

    链接:https://ac.nowcoder.com/acm/contest/3003/C来源:牛客网 题目描述 牛牛刚刚考完了期末,尽管牛牛做答了所有 n 道题目,但他不知道有多少题是正确的. 不过 ...

  8. UVA 11987 - Almost Union-Find 并查集的活用 id化查找

    受教了,感谢玉斌大神的博客. 这道题最难的地方就是操作2,将一个集合中的一个点单独移到另一个集合,因为并查集的性质,如果该点本身作为root节点的话,怎么保证其他点不受影响. 玉斌大神的思路很厉害,受 ...

  9. python-IDE使用

    集成开发工具 集成开发环境(IDE,Integrated Development Environment ) VIM 经典的linux下的文本编辑器,只有小白和的牛会用这个 Emacs linux文本 ...

  10. ACwing算法基础课听课笔记(第一章,基础算法一)(二分)

    二分法: 在看这个视频前,我对于二分法是一头雾水的,又加上这个算法平常从来没写过所以打了一年了还没正式搞过.视频提到ACwing上的一道题,我用自以为聪明的方法去做,结果TLE了,实在丢人,不说了,开 ...