[原]C++新标准之std::chrono::time_point
概览
time_point定义在<chrono>
文件中,用来表示时间点。
类定义
关键代码摘录如下(格式有调整):
- template<class _Clock, class _Duration = typename _Clock::duration>
- class time_point
- {
- public:
- typedef _Clock clock;
- typedef _Duration duration;
- constexpr time_point() : _MyDur(_Duration::zero()) {}
- constexpr explicit time_point(const _Duration& _Other) : _MyDur(_Other) {}
- template<class _Duration2,
- class = typename enable_if<is_convertible<_Duration2, _Duration>::value,void>::type>
- constexpr time_point(const time_point<_Clock, _Duration2>& _Tp) : _MyDur(_Tp.time_since_epoch()) {}
- constexpr _Duration time_since_epoch() const { return (_MyDur); }
- private:
- _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秒钟
- std::this_thread::sleep_for(std::chrono::seconds(10));
- std::this_thread::sleep_until(std::chrono::system_clock::now() + std::chrono::seconds(10));
例2. 计时代码
一个简单的计时代码,展示了std::chrono::high_resolution_clock和std::chrono::duration的用法。
- std::vector<double> v(10'000'007, 0.5);
- auto t1 = std::chrono::high_resolution_clock::now();
- double result = std::accumulate(v.begin(), v.end(), 0.0);
- auto t2 = std::chrono::high_resolution_clock::now();
- std::chrono::duration<double, std::milli> ms = t2 - t1;
- std::cout << std::fixed << "std::accumulate result " << result << " took " << ms.count() << " ms\n";
std::chrono::system_clock::time_point
定义:
- struct system_clock
- {
- using rep = long long;
- // use system_lock as _Clock template parameter
- using time_point = chrono::time_point<system_clock>;
- };
参考资料
- vs源码
- cppreference