On this page
timeit.Timer
Prev: timeit.timeit | Up: timeit
Rel: timeit.default_timer, timeit.repeat, timeit.timeit
Ref: docs.python.org — timeit.Timer
Tags: #public #python3 #stdlib #timeit
Import
import timeit
from timeit import Timer
Signature & Overview
timeit.Timer(stmt='pass', setup='pass', timer=<built-in function perf_counter>, globals=None)
Class for timing execution speed of small code snippets.
Methods & Attributes
Timer.autorange(self, callback=None)— Return the number of loops and time taken so that total time >= 0.2.Timer.print_exc(self, file=None)— Helper to print a traceback from the timed code.Timer.repeat(self, repeat=5, number=1000000)— Call timeit() a few times.Timer.timeit(self, number=1000000)— Time 'number' executions of the main statement.
Usage
import timeit
t = timeit.Timer('sum(xs)', globals={'xs': list(range(100))})
elapsed = t.timeit(number=1000)
assert elapsed >= 0.0
Gotchas
- Call
t.autorange()to pick a sensible iteration count automatically.
REPL Inspect
from timeit import Timer
help(Timer)
Linked from
Related notes
- tuts/python3/stdlib_module/timeit/default_timer Linked here; links to this page; shared tags: #stdlib, #timeit
- tuts/python3/stdlib_module/timeit/repeat Linked here; links to this page; shared tags: #stdlib, #timeit
- tuts/python3/stdlib_module/timeit/timeit Linked here; links to this page; shared tags: #stdlib, #timeit
- tuts/python3/stdlib_module/timeit links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib