原 总结 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. dll调用--出现运行时调用不协调

    "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  T ...

  2. 【LeetCode】验证二叉搜索树

    [问题]给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征:节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是二叉搜 ...

  3. Oracle学习笔记(1)

    1.常用sqlplus命令 (1)查看当前登录用户:show user; (2)切换当前登录用户:conn 用户名/密码 (切换系统用户+as sysdba ); (3)断开当前登录用户:disc; ...

  4. 18 12 30 服务器 Django 的初步使用 环境变量的调整

    1.安装django 1.1.下载Django包 https://www.djangoproject.com/download/https://www.djangoproject.com/m/rele ...

  5. 18 12 23 html 基本学习

    一个html的基本结构如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  6. 程序员用 Python 扒出 B 站那些“惊为天人”的UP主!

    ​ 前言 ! 近期B站的跨年晚会因其独特的创意席卷各大视频网站,给公司带来了极大的正面影响,股价也同时大涨,想必大家都在后悔没有早点买B站的股票: ​ 然而今天我们要讨论的不是B站的跨年晚会,而是B站 ...

  7. composer命令卡慢,使用国内源

    执行composer install.update 和require的时候,遇到卡住不动的情况,可以切换到国内阿里云的源 composer config -g repo.packagist compo ...

  8. WOJ 1542 Countries 并查集转化新点+最短路

    http://acm.whu.edu.cn/land/problem/detail?problem_id=1542 今天做武大的网赛题,哎,还是不够努力啊,只搞出三个 这个题目一看就是个最短路,但是题 ...

  9. 步进电机加减速S曲线算法

    一.Sigmoid 函数 1.1 Sigmoid函数原型 1.2 sigmoid函数波形: 由图形可看出在-10时已经接近于0,一般取值区间在[-5,5]. 1.3 sigmoid函数的导数 转载CS ...

  10. 吴裕雄--天生自然MySQL学习笔记:MySQL 复制表

    如果需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等. 如果仅仅使用CREATE TABLE ... SELECT 命令,是无法实现的. 如何完整的复制MySQL数据表,步骤如下: 使用 ...