What are the uses of std::chrono::high_resolution_clock?

狂风中的少年 提交于 2019-11-28 19:39:44
Howard Hinnant

There are none.

Sorry, my bad.

If you are tempted to use high_resolution_clock, choose steady_clock instead. On libc++ and VS high_resolution_clock is a type alias of steady_clock anyway.

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).

Do use <chrono>. But there are parts of <chrono> that you should avoid.

  • Don't use high_resolution_clock.
  • Avoid uses of .count() and .time_since_epoch() unless there is no other way to get the job done.
  • Avoid duration_cast unless the code won't compile without it, and you desire truncation-towards-zero behavior.
  • 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. :)

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. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!