std :: chrono :: high_resolution_clock有什么用? [英] What are the uses of std::chrono::high_resolution_clock?

查看:144
本文介绍了std :: chrono :: high_resolution_clock有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

起初,我认为它可以用于性能评估。但是 std :: chrono :: high_resolution_clock 可能不稳定( is_steady 可能为 false )。也有人说 std :: chrono :: high_resolution_clock 甚至可能是 std :: chrono :: system_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). 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. 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.

不会将 std :: chrono :: high_resolution_clock 的时间点转换为日历时间,因为它没有 to_time_t 方法。因此,我也无法通过这种类型的时钟获得实时

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. So I can't get the real time with this type of clock either.

然后 std可以做什么: chrono :: high_resolution_clock 用于?

推荐答案

没有。

对不起,我的坏人

如果您想使用 high_resolution_clock ,请选择而不是steady_clock 。在libc ++和VS上, high_resolution_clock steady_clock 的类型别名。

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.

在gcc high_resolution_clock system_clock 的类型别名,我已经看到了 high_resolution_clock :: to_time_t 在该平台上(这是错误的)。

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

执行请使用< chrono> 。但是您应该避免< chrono> 的某些部分。

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


  • Don不要使用 high_resolution_clock

  • 避免使用 .count() .time_since_epoch(),除非没有其他方法可以完成工作。

  • 避免 duration_cast ,除非没有它,否则代码将无法编译,并且您希望截断取零行为。

  • 如果隐式转换编译,请避免使用显式转换语法。

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

这篇关于std :: chrono :: high_resolution_clock有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆