PEP 418: Re-add the pseudo-code for time.perf_counter()

This commit is contained in:
Victor Stinner 2012-04-09 13:21:34 +02:00
parent 94d094d791
commit 954c3684ce
1 changed files with 33 additions and 10 deletions

View File

@ -993,23 +993,46 @@ Issues of a hacked monotonic function:
Deferred API: time.perf_counter()
=================================
Python does not provide a portable "performance counter" clock for benchmarking
or profiling. Each tool has to implement its own heuristic to decide which
clock is the best depending on the OS and on which counters are available.
Performance counter used for benchmarking and profiling. The reference point of
the returned value is undefined so only the difference of consecutive calls is
valid and is number of seconds.
A previous version of the PEP proposed a time.perf_counter() function using
QueryPerformanceCounter() on Windows, time.monotonic(), or falls back to the
system time. This function was not well defined and the idea is deferred.
Pseudo-code::
Proposed names for such function:
def perf_counter():
if perf_counter.use_performance_counter:
if perf_counter.cpu_frequency is None:
try:
perf_counter.cpu_frequency = float(_time.QueryPerformanceFrequency())
except OSError:
# QueryPerformanceFrequency() fails if the installed
# hardware does not support a high-resolution performance
# counter
perf_counter.use_performance_counter = False
else:
return _time.QueryPerformanceCounter() / perf_counter.cpu_frequency
else:
return _time.QueryPerformanceCounter() / perf_counter.cpu_frequency
if perf_counter.use_monotonic:
# Monotonic clock is preferred over system clock
try:
return time.monotonic()
except OSError:
perf_counter.use_monotonic = False
return time.time()
perf_counter.use_performance_counter = (os.name == 'nt')
if perf_counter.use_performance_counter:
perf_counter.cpu_frequency = None
perf_counter.use_monotonic = hasattr(time, 'monotonic')
Other names proposed for time.perf_counter():
* time.hires()
* time.highres()
* time.perf_counter()
* time.timer()
Python source code includes a portable library to get the process time:
`Tools/pybench/systimes.py
Python source code includes a portable library to get the process time (CPU
time): `Tools/pybench/systimes.py
<http://hg.python.org/cpython/file/tip/Tools/pybench/systimes.py>`_.