Skip to content

python/elapsed-without-perf-counter

Rule elapsed-without-perf-counter
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

Measuring a duration with time.time() uses the system clock, which can be set back. Use time.perf_counter(), which only moves forward.

import time
def timed_fit(model: Model, rows: Rows) -> float:
start = time.time()
start = time.perf_counter()
model.fit(rows)
return time.time() - start
return time.perf_counter() - start

The rule reports time.time() minus a name that the same function, or the module, sets from time.time(). It leaves alone a difference with any other value, such as the modified time of a file, because that value is a time of day.

xpd has no automatic fix, so make the change by hand.

time.time() reads the clock of the system, which a person or a time server can set. The Python documentation says it can return "a lower value than a previous call if the system clock has been set back", so a duration measured with it can be wrong and can be negative. time.perf_counter() is "a clock with the highest available resolution to measure a short duration", and it only moves forward. Its value means nothing on its own, so keep time.time() for a time of day that the code stores or prints.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This measures a duration with the clock of the system, which can be set back. Read time.perf_counter() in both places, which only moves forward.

Browse the other rules of the Python pack.