std::system_clock 和 std::steady_clock 的区别?
std::system_clock
和 std::steady_clock
有什么区别?(说明不同结果/行为的示例案例会很棒).
What is the difference between std::system_clock
and std::steady_clock
? (An example case that illustrate different results/behaviours would be great).
如果我的目标是精确测量函数的执行时间(如基准测试),std::system_clock
和 std::steady_clock
之间的最佳选择是什么和 std::high_resolution_clock
?
If my goal is to precisely measure execution time of functions (like a benchmark), what would be the best choice between std::system_clock
, std::steady_clock
and std::high_resolution_clock
?
推荐答案
来自 N3376:
20.11.7.1 [time.clock.system]/1:
20.11.7.1 [time.clock.system]/1:
system_clock
类的对象表示来自系统范围实时时钟的挂钟时间.
Objects of class
system_clock
represent wall clock time from the system-wide realtime clock.
20.11.7.2 [time.clock.steady]/1:
20.11.7.2 [time.clock.steady]/1:
steady_clock
类的对象表示 time_point
的值不会随着物理时间的增加而减少的时钟,并且 time_point
的值以一定的速度增加相对于实时的稳定速率.也就是说,时钟可能不会被调整.
Objects of class
steady_clock
represent clocks for which values oftime_point
never decrease as physical time advances and for which values oftime_point
advance at a steady rate relative to real time. That is, the clock may not be adjusted.
20.11.7.3 [time.clock.hires]/1:
20.11.7.3 [time.clock.hires]/1:
high_resolution_clock
类的对象表示具有最短滴答周期的时钟.high_resolution_clock
可能是 system_clock
或 steady_clock
的同义词.
Objects of class
high_resolution_clock
represent clocks with the shortest tick period.high_resolution_clock
may be a synonym forsystem_clock
orsteady_clock
.
例如,系统范围的时钟可能会受到夏令时等因素的影响,此时列出的未来某个时间点的实际时间实际上可能是过去的时间.(例如在美国,在下降时间后移一小时,因此同一小时会经历两次")但是,steady_clock
不允许受此类事情的影响.
For instance, the system wide clock might be affected by something like daylight savings time, at which point the actual time listed at some point in the future can actually be a time in the past. (E.g. in the US, in the fall time moves back one hour, so the same hour is experienced "twice") However, steady_clock
is not allowed to be affected by such things.
在这种情况下考虑稳定"的另一种方式是在 20.11.3 [time.clock.req]/2 表中定义的要求:
Another way of thinking about "steady" in this case is in the requirements defined in the table of 20.11.3 [time.clock.req]/2:
在表 59 中,C1
和 C2
表示时钟类型.t1
和 t2
是 C1::now()
返回的值,其中返回 t1
的调用发生在调用之前返回 t2
并且这两个调用都发生在 C1::time_point::max()
之前.[ 注意:这意味着 C1
没有在 t1
和 t2
之间回绕.――结尾说明]
In Table 59
C1
andC2
denote clock types.t1
andt2
are values returned byC1::now()
where the call returningt1
happens before the call returningt2
and both of these calls occur beforeC1::time_point::max()
. [ Note: this meansC1
did not wrap around betweent1
andt2
. ―end note ]
表达式:C1::is_steady
返回:const bool
操作语义:true
如果 t1 <= t2
始终为真并且时钟滴答之间的时间是恒定的,否则 false
.
Expression: C1::is_steady
Returns: const bool
Operational Semantics: true
if t1 <= t2
is always true and the time between clock ticks is constant, otherwise false
.
这就是标准对它们的区别的全部内容.
That's all the standard has on their differences.
如果您想进行基准测试,最好的选择可能是 std::high_resolution_clock
,因为您的平台很可能使用高分辨率计时器(例如 QueryPerformanceCounter
code> 在 Windows 上)用于此时钟.但是,如果您要进行基准测试,您真的应该考虑将特定于平台的计时器用于您的基准测试,因为不同的平台对此有不同的处理方式.例如,某些平台可能会提供一些方法来确定程序所需的实际时钟滴答数(独立于在同一 CPU 上运行的其他进程).更好的是,获得一个真正的分析器并使用它.
If you want to do benchmarking, your best bet is probably going to be std::high_resolution_clock
, because it is likely that your platform uses a high resolution timer (e.g. QueryPerformanceCounter
on Windows) for this clock. However, if you're benchmarking, you should really consider using platform specific timers for your benchmark, because different platforms handle this differently. For instance, some platforms might give you some means of determining the actual number of clock ticks the program required (independent of other processes running on the same CPU). Better yet, get your hands on a real profiler and use that.
相关文章