[原]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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App