Python Glossary Русский

all Python 3timeitterm

timeit.Timer

timing

A class for timing one code snippet repeatedly, with the methods timeit(), repeat() and autorange().

Syntax

t = timeit.Timer(stmt='pass', setup='pass', timer=default_timer, globals=None); t.timeit(number=1000000); t.autorange()

It is handy when the same measurement is needed several times: setup runs on every timeit() call, but the code is compiled once. autorange() picks number so the measurement takes at least 0.2 seconds and returns a pair (number, time). print_exc() prints a traceback showing the snippet's source line rather than the internal template.

Examples

import timeit
t = timeit.Timer('"-".join(map(str, range(50)))')
number, total = t.autorange()
print(type(number).__name__, type(total).__name__)  # → int float
print(number)  # → ? зависит от скорости машины
import timeit
t = timeit.Timer(lambda: sorted([3, 1, 2]))
print(t.timeit(number=10) >= 0)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy