On this page
timeit
Prev: time | Up: stdlib_module | Next: tkinter
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — timeit
Tags: #public #python3 #stdlib
Import
import timeit
Overview
Micro-benchmarking harness for comparing the execution speed of small Python expressions and functions (timeit, repeat, Timer, and python -m timeit). Disables garbage collection during measurement by default and runs enough iterations to smooth out OS jitter.
Quick Start
import timeit
elapsed = timeit.timeit("'-'.join(str(n) for n in range(100))", number=1000)
assert elapsed > 0.0
Implementation Walkthrough
1. Comparing Two Implementations with Timer.repeat()
Always report the minimum across repeats (the fastest clean run with the least OS background interference), not the mean:
import timeit
data = list(range(1000))
t_comp = min(timeit.repeat(lambda: [x * 2 for x in data], repeat=5, number=500))
t_map = min(timeit.repeat(lambda: list(map(lambda x: x * 2, data)), repeat=5, number=500))
assert t_comp > 0 and t_map > 0
2. Quick Command-Line Benchmarking
python3 -m timeit -s "data = list(range(1000))" "sum(data)"
Gotchas & Boundaries
- Report
min(repeat(...)), not the average: Spikes are caused by OS scheduling noise, not your code suddenly running slower. - Pass
globals=globals()when benchmarking strings that reference local functions: Or pass a zero-argument callable (lambda: fn(arg)). - Avoid side-effecting setup inside the timed statement: Put one-time data construction in
setup=so you don't benchmark allocation or mutate state across iterations. - For whole-program profiling, use
cProfileinstead oftimeit.
Common Functions
- timeit.default_timer() —
default_timer()— Performance counter for benchmarking. - timeit.repeat() —
repeat(stmt='pass', setup='pass', timer=<built-in function perf_counter>, repeat=5, number=1000000, globals=None)— Convenience function to create Timer object and call repeat method. - timeit.timeit() —
timeit(stmt='pass', setup='pass', timer=<built-in function perf_counter>, number=1000000, globals=None)— Convenience function to create Timer object and call timeit method.
Classes & Objects
- timeit.Timer — Class for timing execution speed of small code snippets. (Methods:
.autorange(),.print_exc(),.repeat(),.timeit())
Linked from
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/time Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tkinter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/annotationlib Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/cProfile links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/doctest Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/pydoc Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/pydoc_data Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/timeit/default_timer Linked here; shared tags: #stdlib