简体   繁体   English

std :: chrono :: high_resolution_clock的用途是什么?

[英]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. 没有了。

Sorry, my bad . 对不起, 我的不好

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_clocksystem_clock的类型别名,我在这个平台上看到过多次使用high_resolution_clock::to_time_t (这是错误的)。

Do use <chrono> . 使用<chrono> But there are parts of <chrono> that you should avoid. 但是你应该避免使用<chrono>部分内容。

  • Don't use high_resolution_clock . 不要使用high_resolution_clock
  • Avoid uses of .count() and .time_since_epoch() unless there is no other way to get the job done. 避免使用.time_since_epoch() .count().time_since_epoch()除非没有其他方法可以完成工作。
  • Avoid duration_cast unless the code won't compile without it, and you desire truncation-towards-zero behavior. 避免使用duration_cast除非代码在没有它的情况下不能编译,并且您希望截断为零行为。
  • Avoid explicit conversion syntax if an implicit conversion compiles. 如果隐式转换编译,则避免使用显式转换语法。

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.

相关问题 在类中使用std :: chrono :: high_resolution_clock播种std :: mt19937的正确方法是什么? - What is the proper way of seeding std::mt19937 with std::chrono::high_resolution_clock inside a class? std :: chrono :: high_resolution_clock-时间测量 - std::chrono::high_resolution_clock - time measurement 使用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 C ++ 11中的std :: chrono :: high_resolution_clock :: now()的类型是什么? - What is the type of std::chrono::high_resolution_clock::now() in C++11? 时间差与std :: chrono :: system_clock / std :: chrono :: high_resolution_clock - time differences with std::chrono::system_clock / std::chrono::high_resolution_clock std::chrono::system_clock 与 std::chrono::high_resolution_clock 行为 - std::chrono::system_clock vs std::chrono::high_resolution_clock behavior chrono :: high_resolution_clock延迟不一致 - Inconsistent chrono::high_resolution_clock delay Android NDK计时时代不正确(std :: chrono :: high_resolution_clock) - Android NDK chrono epoch is not correct (std::chrono::high_resolution_clock) 比较rdtsc clock和c ++ 11 std :: chrono :: high_resolution_clock产生的时间测量结果 - Comparing the time measured results produced by rdtsc clock and c++11 std::chrono::high_resolution_clock
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM