码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-08-15 00:00:31      阅读:1364      评论:0      收藏:0      [点我收藏+]

标签:float   休眠   ddd   系统时间   dde   ant   uil   targe   reboot   

 

 

概览

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

类定义

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

  1. template<class _Clock, class _Duration = typename _Clock::duration> 
  2. class time_point 
  3. { 
  4. public
  5. typedef _Clock clock; 
  6. typedef _Duration duration; 
  7.  
  8. constexpr time_point() : _MyDur(_Duration::zero()) {} 
  9.  
  10. constexpr explicit time_point(const _Duration& _Other) : _MyDur(_Other) {} 
  11.  
  12. template<class _Duration2, 
  13. class = typename enable_if<is_convertible<_Duration2, _Duration>::value,void>::type> 
  14. constexpr time_point(const time_point<_Clock, _Duration2>& _Tp) : _MyDur(_Tp.time_since_epoch()) {} 
  15.  
  16. constexpr _Duration time_since_epoch() const { return (_MyDur); } 
  17.  
  18. private
  19. _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.  
  3. 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. { 
  3. using rep = long long
  4. // use system_lock as _Clock template parameter 
  5. using time_point = chrono::time_point<system_clock>; 
  6. }; 

参考资料

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

标签:float   休眠   ddd   系统时间   dde   ant   uil   targe   reboot   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!