[英]What are the uses of std::chrono::high_resolution_clock?
At first I thought it can be used for performance measurements. 起初我认为它可以用于性能测量。 But it is said that
std::chrono::high_resolution_clock
may be not steady ( is_steady
may be false
). 但据说
std::chrono::high_resolution_clock
is_steady
std::chrono::high_resolution_clock
is_steady
可能不稳定( is_steady
可能是false
)。 It is also said that std::chrono::high_resolution_clock
may even be an alias of std::chrono::system_clock
which is generally not steady. 据说
std::chrono::high_resolution_clock
甚至可能是std::chrono::system_clock
的别名,它通常不稳定。 So I can't measure time intervals with this type of clock because at any moment the clock may be adjusted and my measurements will be wrong. 所以我无法用这种类型的时钟测量时间间隔,因为在任何时刻都可能调整时钟并且我的测量结果是错误的。
At the same time I can't convert time points of std::chrono::high_resolution_clock
to calendar time because it doesn't have to_time_t
method. 同时我无法将
std::chrono::high_resolution_clock
to_time_t
时间点转换为日历时间,因为它没有to_time_t
方法。 So I can't get the real time with this type of clock either. 所以我也无法用这种类型的时钟获得实时 。
Then what can std::chrono::high_resolution_clock
be used for? 那么
std::chrono::high_resolution_clock
可以用于什么?
There are none. 没有了。
If you are tempted to use high_resolution_clock
, choose steady_clock
instead. 如果你想使用
high_resolution_clock
,选择steady_clock
代替。 On libc++ and VS high_resolution_clock
is a type alias of steady_clock
anyway. 着libc ++和VS
high_resolution_clock
是一个类型别名steady_clock
反正。
On gcc high_resolution_clock
is a type alias of system_clock
and I've seen more than one use of high_resolution_clock::to_time_t
on this platform (which is wrong). 在gcc上,
high_resolution_clock
是system_clock
的类型别名,我在这个平台上看到过多次使用high_resolution_clock::to_time_t
(这是错误的)。
Do use <chrono>
. 请使用
<chrono>
。 But there are parts of <chrono>
that you should avoid. 但是你应该避免使用
<chrono>
部分内容。
high_resolution_clock
. high_resolution_clock
。 .count()
and .time_since_epoch()
unless there is no other way to get the job done. .time_since_epoch()
.count()
和.time_since_epoch()
除非没有其他方法可以完成工作。 duration_cast
unless the code won't compile without it, and you desire truncation-towards-zero behavior. duration_cast
除非代码在没有它的情况下不能编译,并且您希望截断为零行为。 I would suggest that single uses of the high res clock could lie. 我建议单独使用高分辨率时钟就可以了 。
If your algorithm makes use of many measurements then any error should average-out and you should be able to measure very small intervals. 如果您的算法使用了许多测量值,那么任何误差都应该是平均值,并且您应该能够测量非常小的间隔。
I've used the high resolution clock to build timelines in network events where otherwise my intervals would have all been 0
. 我已经使用高分辨率时钟在网络事件中构建时间线,否则我的间隔将全部为
0
。 :) :)
Finally, consider that while your system clock might not be steady, if your high res clock is stead, that greatly simplifies some games you have to play to make sure time always flows forwards. 最后,考虑到虽然你的系统时钟可能不稳定,但如果你的高分辨率时钟是稳定的, 那么大大简化了你必须玩的一些游戏,以确保时间总是向前流动。
In summary, yeah, it can fail, but when you have it, it's very helpful. 总之,是的,它可能会失败,但是当你拥有它时,它会非常有帮助。 Lots of samples are your friends.
很多样品都是你的朋友。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.