std :: system_clock和std :: steady_clock之间的区别? [英] Difference between std::system_clock and 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
?
推荐答案
> 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
的同义词。 / p>
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中定义的要求: / p>
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()
其中在返回t2
的调用之前发生返回t1
的调用,并且这两个调用发生在C1 :: time_point :: max()
。 [注意:这意味着C1
没有在t1
和t2
。 -end note]
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
始终为真,时钟间隔之间的时间是常数,否则
false
。
b $ b
这是所有的标准对他们的差异。
That's all the standard has on their differences.
如果你想做基准,你最好的赌注可能是 std :: high_resolution_clock
,因为这可能是您的平台使用高分辨率定时器(例如 QueryPerformanceCounter
在Windows上)。 (编辑:MSVC ++,从Visual Studio 2013,还没有使用 QueryPerformanceCounter
这个时钟:()但是,如果你是基准测试,你应该考虑使用平台特定例如,一些平台可能会提供一些方法来确定程序需要的实际时钟周期数(独立于在同一个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. ( MSVC++, as of Visual Studio 2013, does not yet use QueryPerformanceCounter
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.
这篇关于std :: system_clock和std :: steady_clock之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!