理解 time.perf_counter() 和 time.process_time() [英] Understanding time.perf_counter() and time.process_time()

查看:142
本文介绍了理解 time.perf_counter() 和 time.process_time()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对新函数 time.perf_counter()time.process_time() 有一些疑问.

I have some questions about the new functions time.perf_counter() and time.process_time().

对于前者,来自文档:

返回性能计数器的值(以小数秒为单位),即具有最高可用分辨率的时钟以测量短持续时间.它确实包括睡眠期间经过的时间,并且是系统范围的.返回值的参考点未定义,因此只有连续调用结果之间的差异才有效.

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to 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.

这个最高分辨率"在所有系统上都一样吗?或者它总是稍微取决于,例如,我们使用 linux 还是 windows?
问题来自阅读 time.time() 的文档,它说并非所有系统都提供比 1 秒更好的时间精度",因此它们如何提供更好和更高的分辨率现在?

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?

关于后者,time.process_time():

返回当前进程的系统和用户 CPU 时间总和的值(以小数秒为单位). 不包括睡眠期间经过的时间.根据定义,它是流程范围的.返回值的参考点未定义,因此只有连续调用结果之间的差异才有效.

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?

推荐答案

在这种情况下,时间"有两种截然不同的类型:绝对时间和相对时间.

There are two distincts types of 'time', in this context: absolute time and relative time.

绝对时间是真实世界时间",它由 time.time() 返回,我们都习惯处理它.它通常是从过去的固定时间点(例如 UNIX 纪元 01/01/1970 UTC 时间 00:00:00)以至少 1 秒的分辨率进行测量的.现代系统通常提供毫秒或微秒的分辨率.它由大多数计算机上的专用硬件维护,RTC(实时时钟)电路通常由电池供电,因此系统可以跟踪上电之间的实时时间.这个真实世界时间"也会根据您的位置(时区)和季节(夏令时)进行修改,或者表示为与 UTC(也称为 GMT 或祖鲁时间)的偏移量.

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.perf_countertime.process_time返回.这种类型的时间与现实世界的时间没有明确的关系,因为这种关系是特定于系统和实现的.它只能用于测量时间间隔,即与两个瞬间之间经过的时间成正比的无单位值.这主要用于评估相对性能(例如,此版本的代码是否比该版本的代码运行得更快).

Secondly, there is relative time, which is returned by time.perf_counter and 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).

在现代系统中,它是使用 CPU 计数器测量的,该计数器以与 CPU 硬件时钟相关的频率单调递增.计数器分辨率高度依赖于系统的硬件,在大多数情况下,该值无法与现实世界的时间可靠地相关联,甚至无法在系统之间进行比较.此外,每次 CPU 上电或复位时,计数器值都会复位.

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.

time.perf_counter 返回计数器的绝对值.time.process_time 是从 CPU 计数器派生的值,但仅在给定进程在 CPU 上运行时更新,并且可以分解为用户时间",即进程本身是在CPU上运行的,还有'系统时间',就是操作系统内核代表进程在CPU上运行的时间.

time.perf_counter returns the absolute value of the counter. time.process_time is 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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