Python 理解 time.perf_counter() 和 time.process_time()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25785243/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding time.perf_counter() and time.process_time()
提问by zer0uno
I have some questions about the new functions time.perf_counter()and time.process_time().
我对新功能time.perf_counter()和time.process_time().
For the former, from the documentation:
对于前者,来自文档:
Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolutionto measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
返回性能计数器的值(以秒为单位),即具有最高可用分辨率的时钟以测量短持续时间。它确实包括睡眠期间经过的时间,并且是系统范围的。返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。
Is this 'highest resolution' the same on all systems? Or does it always slightly depend if, for example, we use linux or windows?
The question comes from the fact the reading the documentation of time.time()it says that 'not all systems provide time with a better precision than 1 second' so how can they provide a better and higher resolution now?
这个“最高分辨率”在所有系统上都一样吗?或者它总是稍微取决于,例如,我们使用 linux 还是 windows?
问题来自阅读time.time()它的文档说“并非所有系统都提供比 1 秒更好的时间精度”的事实,那么它们现在如何提供更好和更高的分辨率?
About the latter, time.process_time():
关于后者,time.process_time():
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process.It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
返回当前进程的系统和用户 CPU 时间总和的值(以秒为单位)。它不包括睡眠期间经过的时间。根据定义,它是流程范围的。返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。
I don't understand, what are those 'system time' and 'user CPU time'? What's the difference?
我不明白,那些“系统时间”和“用户 CPU 时间”是什么?有什么不同?
采纳答案by isedev
There are two distincts types of 'time', in this context: absolute time and relative time.
在这种情况下,有两种不同类型的“时间”:绝对时间和相对时间。
Absolute time is the 'real-world time', which is returned by time.time()and which we are all used to deal with. It is usually measured from a fixed point in time in the past (e.g. the UNIX epoch of 00:00:00 UTC on 01/01/1970) at a resolution of at least 1 second. Modern systems usually provide milli- or micro-second resolution. It is maintained by the dedicated hardware on most computers, the RTC (real-time clock) circuit is normally battery powered so the system keeps track of real time between power ups. This 'real-world time' is also subject to modifications based on your location (time-zones) and season (daylight savings) or expressed as an offset from UTC (also known as GMT or Zulu time).
绝对时间是“真实世界时间”,它是由time.time()它返回的,我们都习惯于处理它。它通常是从过去的固定时间点(例如 1970 年 1 月 1 日 UTC 时间 00:00:00 的 UNIX 纪元)以至少 1 秒的分辨率测量的。现代系统通常提供毫秒或微秒的分辨率。它由大多数计算机上的专用硬件维护,RTC(实时时钟)电路通常由电池供电,因此系统可以跟踪上电之间的实时时间。这个“真实世界时间”也会根据您的位置(时区)和季节(夏令时)进行修改,或者表示为与 UTC(也称为 GMT 或祖鲁时间)的偏移量。
Secondly, there is relative time, which is returned by time.perf_counterand time.process_time. This type of time has no defined relationship to real-world time, in the sense that the relationship is system and implementation specific. It can be used only to measure time intervals, i.e. a unit-less value which is proportional to the time elapsed between two instants. This is mainly used to evaluate relative performance (e.g. whether this version of code runs faster than that version of code).
其次是相对时间,由time.perf_counterand返回time.process_time。这种类型的时间与现实世界的时间没有明确的关系,因为这种关系是特定于系统和实现的。它只能用于测量时间间隔,即与两个瞬间之间经过的时间成正比的无单位值。这主要用于评估相对性能(例如,此版本的代码是否比该版本的代码运行得更快)。
On modern systems, it is measured using a CPU counter which is monotonically increased at a frequency related to CPU's hardware clock. The counter resolution is highly dependent on the system's hardware, the value cannot be reliably related to real-world time or even compared between systems in most cases. Furthermore, the counter value is reset every time the CPU is powered up or reset.
在现代系统上,它是使用 CPU 计数器测量的,该计数器以与 CPU 硬件时钟相关的频率单调增加。计数器分辨率高度依赖于系统的硬件,在大多数情况下,该值无法与现实世界的时间可靠地相关联,甚至无法在系统之间进行比较。此外,每次 CPU 上电或复位时,计数器值都会复位。
time.perf_counterreturns the absolute value of the counter. time.process_timeis a value which is derived from the CPU counter but updated only when a given process is running on the CPU and can be broken down into 'user time', which is the time when the process itself is running on the CPU, and 'system time', which is the time when the operating system kernel is running on the CPU on behalf on the process.
time.perf_counter返回计数器的绝对值。time.process_time是从 CPU 计数器派生的值,但仅在给定进程在 CPU 上运行时更新,并且可以分解为“用户时间”,即进程本身在 CPU 上运行的时间和“系统时间” time',这是操作系统内核代表进程在 CPU 上运行的时间。

