New submission from Ken Healy <healykb+pythonb...@gmail.com>:

The documentation for time.perf_counter() indicates it should return a 
system-wide value: https://docs.python.org/3/library/time.html#time.perf_counter

This is not true on Windows, as a process-specific offset is subtracted from 
the underlying QueryPerformanceCounter() value. The code comments indicate this 
is to reduce precision loss: 
https://github.com/python/cpython/blob/master/Python/pytime.c#L995-L997

This is relevant in multiprocess applications, where accurate timing is 
required across multiple processes. Here is a simple test case:

-----------------------------------------------------------
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()))
-----------------------------------------------------------

Output:
-----------------------------------------------------------
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
-----------------------------------------------------------

See my stackoverflow question for a comparison with Linux: 
https://stackoverflow.com/questions/56502111/should-time-perf-counter-be-consistent-across-processes-in-python-on-windows

----------
assignee: docs@python
components: Documentation, Library (Lib), Windows
messages: 345057
nosy: docs@python, kh90909, paul.moore, steve.dower, tim.golden, vstinner, 
zach.ware
priority: normal
severity: normal
status: open
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37205>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to