Python 3.3+timefunction
time.perf_counter()
clocks and timing
A high-resolution counter in seconds for measuring short intervals: how long a piece of code took.
Syntax
time.perf_counter() -> float
The standard way to time code: t0 = perf_counter() before, the difference after. It counts real (wall) time, including sleep and waiting for input; pure CPU time comes from process_time(). A single measurement is noisy — repeat micro-benchmarks many times or use timeit. In CPython 3.13+ it is the same clock as monotonic(), but portable code should not rely on that.
Examples
import time
t0 = time.perf_counter()
total = sum(range(100_000))
elapsed = time.perf_counter() - t0
print(elapsed >= 0) # → True
print(f'{elapsed:.4f} с') # → ? 0.0012 с — зависит от машины
See also
Open in the full glossarydocs.python.orgReport an inaccuracy