Python Glossary Русский

all Python 3timeitterm

timeit

module

Measures how long small code snippets take to run, repeating them many times with the best timer.

Syntax

import timeit

Use timeit to compare two ways of writing the same thing: it repeats a snippet thousands of times and disables garbage collection while timing. A single measurement with time.perf_counter() is noisy, and timeit averages that noise out. A common mistake is forgetting that the result is the total time of all number runs, not of one; to profile a whole program use cProfile.

Examples

import timeit
t = timeit.timeit("sum(range(100))", number=1000)
print(t > 0)  # → True
print(f"{t:.5f} с на 1000 повторов")  # → ? зависит от машины

See also

Open in the full glossarydocs.python.orgReport an inaccuracy