time.perf_counter() 应该在 Windows 上的 Python 中跨进程保持一致吗?

2024-03-22

UPDATE:此错误的修复已提交,并将在 Python 3.10 中首次亮相,预计将于 2021 年 10 月发布。请参阅错误报告 https://bugs.python.org/issue37205#msg381107了解详情。


的文档time.perf_counter() https://docs.python.org/3.7/library/time.html#time.perf_counter表明它是全系统

time.性能计数器() → float

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

我的解释有误吗全系统包括跨流程的一致性?

如下所示,在Linux上看起来是一致的,但在Windows上却不一致。此外,Python 3.6 的 Windows 行为与 3.7 显着不同。

如果有人能指出涵盖此行为的文档或错误报告,我将不胜感激。

测试用例

import concurrent.futures
import time

def worker():
    return time.perf_counter()

if __name__ == '__main__':
    pool = concurrent.futures.ProcessPoolExecutor()
    futures = []
    for i in range(3):
        print('Submitting worker {:d} at time.perf_counter() == {:.3f}'.format(i, time.perf_counter()))
        futures.append(pool.submit(worker))
        time.sleep(1)

    for i, f in enumerate(futures):
        print('Worker {:d} started at time.perf_counter() == {:.3f}'.format(i, f.result()))

Windows 7 上的结果

C:\...>Python36\python.exe -VV
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)]

C:\...>Python36\python.exe perf_counter_across_processes.py
Submitting worker 0 at time.perf_counter() == 0.000
Submitting worker 1 at time.perf_counter() == 1.169
Submitting worker 2 at time.perf_counter() == 2.170
Worker 0 started at time.perf_counter() == 0.000
Worker 1 started at time.perf_counter() == 0.533
Worker 2 started at time.perf_counter() == 0.000

C:\...>Python37\python.exe -VV
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]

C:\...>Python37\python.exe perf_counter_across_processes.py
Submitting worker 0 at time.perf_counter() == 0.376
Submitting worker 1 at time.perf_counter() == 1.527
Submitting worker 2 at time.perf_counter() == 2.529
Worker 0 started at time.perf_counter() == 0.380
Worker 1 started at time.perf_counter() == 0.956
Worker 2 started at time.perf_counter() == 1.963

为了简洁起见,我省略了 Windows 上的进一步结果,但在 Windows 8.1 上观察到了相同的行为。此外,Python 3.6.7 的行为与 3.6.8 相同,而 Python 3.7.1 的行为与 3.7.3 相同。

Ubuntu 18.04.1 LTS 上的结果

$ python3 -VV
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0]

$ python3 perf_counter_across_processes.py 
Submitting worker 0 at time.perf_counter() == 2075.896
Submitting worker 1 at time.perf_counter() == 2076.900
Submitting worker 2 at time.perf_counter() == 2077.903
Worker 0 started at time.perf_counter() == 2075.900
Worker 1 started at time.perf_counter() == 2076.902
Worker 2 started at time.perf_counter() == 2077.905

$ python3.7 -VV
Python 3.7.1 (default, Oct 22 2018, 11:21:55) 
[GCC 8.2.0]

$ python3.7 perf_counter_across_processes.py 
Submitting worker 0 at time.perf_counter() == 1692.514
Submitting worker 1 at time.perf_counter() == 1693.518
Submitting worker 2 at time.perf_counter() == 1694.520
Worker 0 started at time.perf_counter() == 1692.517
Worker 1 started at time.perf_counter() == 1693.519
Worker 2 started at time.perf_counter() == 1694.522

在 Windows 中,time.perf_counter基于WINAPIQueryPerformanceCounter。该计数器是系统范围的。有关更多信息,请参阅获取高分辨率时间戳 https://learn.microsoft.com/en-us/windows/desktop/SysInfo/acquiring-high-resolution-time-stamps.

也就是说,perf_counter在 Windows 中返回一个与进程启动值相关的值。因此它不是一个系统范围的值。这样做是为了减少将整数值转换为整数时的精度损失float,其精度只有 15 位小数。在大多数情况下不需要使用相对值,只需要微秒精度。应该有一个可选参数来查询真实的 QPC 计数器值,特别是对于perf_counter_ns在 3.7+ 中。

关于返回的不同初始值perf_counter在 3.6 与 3.7 中,随着时间的推移,实现发生了一些变化。在3.6.8中,perf_counter实施于模块/timemodule.c https://github.com/python/cpython/blob/v3.6.8/Modules/timemodule.c#L80,因此当time模块首先导入并初始化,这就是为什么您看到第一个结果为 0.000 秒。在最近的版本中,它在 Python 的 C API 中单独实现。例如,参见“Python/pytime.c” https://github.com/python/cpython/blob/v3.8.0b1/Python/pytime.c#L937在最新的 3.8 测试版中。在这种情况下,当Python代码调用时time.perf_counter(),计数器的增量远远超过了启动值。

这是基于 ctypes 的替代实现,它使用系统范围的 QPC 值而不是相对值。

import sys

if sys.platform != 'win32':
    from time import perf_counter
    try:
        from time import perf_counter_ns
    except ImportError:
        def perf_counter_ns():
            """perf_counter_ns() -> int

            Performance counter for benchmarking as nanoseconds.
            """
            return int(perf_counter() * 10**9)
else:
    import ctypes
    from ctypes import wintypes

    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

    kernel32.QueryPerformanceFrequency.argtypes = (
        wintypes.PLARGE_INTEGER,) # lpFrequency

    kernel32.QueryPerformanceCounter.argtypes = (
        wintypes.PLARGE_INTEGER,) # lpPerformanceCount

    _qpc_frequency = wintypes.LARGE_INTEGER()
    if not kernel32.QueryPerformanceFrequency(ctypes.byref(_qpc_frequency)):
        raise ctypes.WinError(ctypes.get_last_error())
    _qpc_frequency = _qpc_frequency.value

    def perf_counter_ns():
        """perf_counter_ns() -> int

        Performance counter for benchmarking as nanoseconds.
        """
        count = wintypes.LARGE_INTEGER()
        if not kernel32.QueryPerformanceCounter(ctypes.byref(count)):
            raise ctypes.WinError(ctypes.get_last_error())
        return (count.value * 10**9) // _qpc_frequency

    def perf_counter():
        """perf_counter() -> float

        Performance counter for benchmarking.
        """
        count = wintypes.LARGE_INTEGER()
        if not kernel32.QueryPerformanceCounter(ctypes.byref(count)):
            raise ctypes.WinError(ctypes.get_last_error())
        return count.value / _qpc_frequency

QPC 的分辨率通常为 0.1 微秒。 AfloatCPython 中的精度为 15 位小数。所以这个实现perf_counter正常运行时间约为 3 年,符合 QPC 分辨率。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

time.perf_counter() 应该在 Windows 上的 Python 中跨进程保持一致吗? 的相关文章

随机推荐