原 总结 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. css div框加小箭头

    <div class="con">    <div class="arrow"> </div></div> .c ...

  2. hibernate.hbm.xml必须必须配置主键

    hibernate.hbm.xml必须必须配置主键 <id name="XXid" type="java.lang.long" column=" ...

  3. EF Core的安装及入门

    一.环境准备 1.开发环境:.NET Core 3.1 2.IDE工具:Visual Studio 2019 3.数据库:SQL Server 2012 二.EF Core的安装 1.新建一个项目,如 ...

  4. 用UICollectionView实现上下轮播的案例

    // //  RecommendNewsCell.swift //  XMLYFM // //  Created by Domo on 2018/8/2. //  Copyright © 2018年 ...

  5. 留学Essay写作做到精准表达很关键

    很多留学生在essay写作中可以迅速想到合理的中文论点.可是,写出来的英文论点却漏洞百出,不忍直视.在essay写作中我们要如何精准地用英文写出自己内心的独白呢?除了咨询老师,靠自己一样能做到! 1引 ...

  6. 吴裕雄--天生自然 JAVASCRIPT开发学习:数据类型

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. DRF源码-serializers

    class BaseSerializer(Field) """ The BaseSerializer class provides a minimal class whi ...

  8. 十分简明易懂的FFT(快速傅里叶变换)

    https://blog.csdn.net/enjoy_pascal/article/details/81478582 FFT前言快速傅里叶变换 (fast Fourier transform),即利 ...

  9. 运行xv6

    我们使用Qemu在Ubuntu下运行 1. 安装Qemu sudo apt-get install qemu 执行 qemu-system-i386 ,如果弹出Qemu界面说明安装成功了 2. 编译x ...

  10. Android前后台切换的监听

    本文参考这位哥们:https://juejin.im/post/5b87f409e51d4538b0640f58 首先写两个类文件ActivityLifecycleCallbacksAdapter.L ...