简体   繁体   English

std::chrono::system_clock 与 std::chrono::high_resolution_clock 行为

[英]std::chrono::system_clock vs std::chrono::high_resolution_clock behavior

Consider the following code snippet:考虑以下代码片段:

#include <chrono>
#include <cassert>

int main()
{
     auto result1 = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
     auto result2 = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch());
     assert((result2.count() - result1.count()) < 10);
}

I am expecting that the difference in counts between the two values should be minimal (ideally less than a second).我期望两个值之间的计数差异应该是最小的(最好小于一秒)。 But with VS2015, the difference in count is of the order of Billions of seconds.但是在 VS2015 中,计数的差异是数十亿秒的数量级。 How is this possible?这怎么可能?

The reason it asserts for you is because high_resolution_clock is allowed to (and often does) have a different epoch than system_clock .它为您断言的原因是因为high_resolution_clock被允许(并且通常确实)具有与system_clock不同的纪元。

It is the de facto standard (not specified but portable) that system_clock 's epoch is measuring time since 1970-01-01 00:00:00 UTC, neglecting leap seconds 1 . system_clock的纪元是从 1970-01-01 00:00:00 UTC 开始测量时间的事实标准(未指定但可移植),忽略闰秒1

There is no de facto standard for high_resolution_clock . high_resolution_clock没有事实上的标准。 On gcc high_resolution_clock is a typedef for system_clock , and so on gcc platforms, you'll notice perfect synchronization.在 gcc high_resolution_clocksystem_clock的 typedef,在 gcc 平台上,你会注意到完美的同步。 On VS and libc++ high_resolution_clock is a typedef for steady_clock .在VS)和libc(++ high_resolution_clock是一个typedef为steady_clock

For me, the epoch of steady_clock is whenever the computer booted up.对我来说, steady_clock的时代是计算机启动时。

Here is a video tutorial for <chrono> .这是<chrono>的视频教程。 It covers lots of issues, including this one, and is about an hour long.它涵盖了很多问题,包括这个问题,大约一个小时。


1 The de facto standard for system_clock has been made official for C++20 . 1 system_clock事实标准已成为 C++20 的官方标准。

Objects of type system_clock represent wall clock time from the system-wide realtime clock. system_clock类型的对象表示来自系统范围实时时钟的挂钟时间。 Objects of type sys_time<Duration> measure time since 1970-01-01 00:00:00 UTC excluding leap seconds. sys_time<Duration>类型的对象测量自 1970-01-01 00:00:00 UTC 以来的时间,不包括闰秒。 This measure is commonly referred to as Unix time .此度量通常称为Unix 时间 This measure facilitates an efficient mapping between sys_time and calendar types ([time.cal]).此度量有助于sys_time和日历类型 ([time.cal]) 之间的有效映射。 [ Example: sys_seconds{sys_days{1970y/January/1}}.time_since_epoch() is 0s . [示例: sys_seconds{sys_days{1970y/January/1}}.time_since_epoch()0s
sys_seconds{sys_days{2000y/January/1}}.time_since_epoch() is 946'684'800s , which is 10'957 * 86'400s . sys_seconds{sys_days{2000y/January/1}}.time_since_epoch()946'684'800s ,即10'957 * 86'400s — end example ] — 结束示例]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 时间差与std :: chrono :: system_clock / std :: chrono :: high_resolution_clock - time differences with std::chrono::system_clock / std::chrono::high_resolution_clock 我怎么知道我的 `std::chrono::high_resolution_clock` 对应于 `steady_clock` 或 `system_clock` 的别名,或其他? - How can I know my `std::chrono::high_resolution_clock` correspond to the alias of `steady_clock` or `system_clock`, or other? std :: chrono :: high_resolution_clock的用途是什么? - What are the uses of std::chrono::high_resolution_clock? 使用std :: chrono :: high_resolution_clock时,“ operator =”不匹配 - No match for “operator =” when using std::chrono::high_resolution_clock 基于 std::chrono::high_resolution_clock 的帧定时器 - std::chrono::high_resolution_clock based frame timer std :: chrono :: high_resolution_clock-时间测量 - std::chrono::high_resolution_clock - time measurement std::chrono::system_clock + now() 到毫秒并返回不同的行为 - std::chrono::system_clock + now() to milliseconds and back different behavior std :: chrono :: system_clock和持续时间<double> - std::chrono::system_clock and duration<double> 在C ++实现中std :: chrono :: system_clock vs std :: chrono :: steady_clock的精度是多少? - Precision of std::chrono::system_clock vs std::chrono::steady_clock across C++ implementations? std::chrono::system_clock 和 C 时间 - std::chrono::system_clock and C time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM