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

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

概览

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

类定义

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

  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::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_clock,system_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_clock是steady_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_clock和std::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
  6. }; 

参考资料

  • vs源码
  • cppreference

转载于:https://www.cnblogs.com/bianchengnan/p/9478806.html

你可能感兴趣的:([原]C++新标准之std::chrono::time_point)