[原]C++新标准之std::chrono::time_point

原 总结 STL 标准库 chrono time_point ratio

    • 概览
    • 类定义
      • 总结
      • 思考
    • 拓展
      • system_clock
      • steady_clock
      • high_resolution_clock
    • 例子
    • 参考资料

概览

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. }; 

参考资料

原文地址:https://www.cnblogs.com/bianchengnan/p/9478806.html

时间: 2024-07-31 20:28:47

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

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

原 总结 C++11 chrono duration ratio 概览 std::chrono::duration 描述 类定义 duration_cast()分析 预定义的duration 示例代码 参考资料 概览 c++新标准提供了新的线程库,最近在写测试代码的时候需要让当前线程休眠,之前直接调用windows提供的Sleep()就好了,新标准中可以使用std::this_thread::sleep_for()或者std::this_thread::sleep_until() 来实现休眠.其

C++11 std::chrono库详解

所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又要出新标准了,好期待啊! 要使用chrono库,需要#include<chrono>,其所有实现均在std::chrono namespace下.注意标准库里面的每个命名空间代表了一个独立的概念.所以下文中的概念均以命名空间的名字表示! chrono是一个模版库,使用简单,功能强大,只需要理解三个

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

一.传统的获取系统时间的方法 传统的C++获取时间的方法需要分平台来定义.相信百度代码也不少. 我自己写了下,如下. const std::string getCurrentSystemTime() { if (PLATFORM_ANDROID || PLATFORM_IOS) { struct timeval s_now; struct tm* p_tm; gettimeofday(&s_now,NULL); p_tm = localtime((const time_t*)&s_now.

关注C++细节——C++11新标准之decltype的使用注意

c++11新特性--decltype decltype是C++11添加的一个新的关键字,目的是选择并返回操作数的数据类型,重要的是,在此过程中编译器分析表达式并得到它的类型,却不实际计算表达式的值. 对于内置类型的对象,使用decltype很直观,但当参数为复合类型的时候就应该注意一些使用细节问题. 1.当decltype作用于数组的时候就应该小心了,本文作者(CSDN   iaccepted). intiarr[10] = {0}; decltype(iarr)ib; 这个时候ib的定义等价于

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

c++11 标准库函数 std::move 和 完美转发 std::forward #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <vector> #include <map> // C++中还有一个被广泛认同的说法,那就是可以取地址的.有名字的就是左值,反之,不能取地址的.没有名字的就是右值. // 相对于左值,右值表示字面常量.表达式.函数的非

多态实现的原理------新标准c++程序设计

"多态"的关键在于通过基类指针或引用调用一个虚函数时,编译时不确定到底调用的是基类还是派生类的函数,运行时才确定.例子: #include<iostream> using namespace std; class A{ public: int i; virtual void func(){}; virtual void func2(){}; //如果为只有一个去掉 virtual 关键字即virtual void func2(){};变为 void func2(){}; 输

《Javascript权威指南》学习笔记之十九--HTML5 DOM新标准---处理文档元信息和管理交互能力

一.了解DOM 1.DOM是Document Object Model的缩写,即文档对象类型,是文档在内存中的表示形式,是一个应用程序接口,定义了文档的逻辑结构以及一套访问和处理文档的方法. 2.HTML DOM与Core DOM的区别:前者提供了大量的方法和属性,与现有的程序模型一致,更便于脚本的编写者控制. 二.document对象 使用window.document属性返回一个document对象,代表当前window内加载的文档.window可以省略.winName.document返回

C++11新标准:nullptr关键字

一.nullptr的意义 1.NULL在C中的定义 #define NULL (void*)0 2.NULL在C++中的定义 #ifndef NULL #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *)0) #endif #endif 3.为什么C++和C中NULL定义不一样呢? void foo(int a) { cout<<"This is int"<<endl; } void foo

C++11新标准:decltype关键字

一.decltype意义 有时我们希望从表达式的类型推断出要定义的变量类型,但是不想用该表达式的值初始化变量(如果要初始化就用auto了).为了满足这一需求,C++11新标准引入了decltype类型说明符,它的作用是选择并返回操作数的数据类型,在此过程中,编译器分析表达式并得到它的类型,却不实际计算表达式的值. 二.decltype用法 1.基本用法 int getSize(); int main(void) { int tempA = 2; /*1.dclTempA为int*/ declty