Python timeit times small snippets from the command line or code, avoiding common timing traps
Answer: Use the command-line interface, python -m timeit "...", to time small bits of Python code. From code, call timeit.timeit(stmt, setup, number=10000) or timeit.repeat(...) and look at the min() of the results.
Examples
python -m timeit "'-'.join(str(n) for n in range(100))"
# 10000 loops, best of 5: 30.2 usec per loop
import timeit
timeit.timeit('"-".join(map(str, range(100)))', number=10000)
min(timeit.repeat("sorted(data)", setup="data=list(range(1000))", number=1000))
timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()
Command-line options
-n Nsets how many times to execute the statement. Without -n, timeit tries increasing loop numbers until the total time is at least--target-time(default: 0.2 seconds).-r Nsets how many times to repeat the timer (default 5).-s Sgives a setup statement executed once initially.-pmeasures process time, not wallclock time.
Things to know
timeit()determines the number of repetitions automatically only in the command-line interface; the Python function defaults tonumber=1000000.- The execution time of setup is excluded from the overall timed execution run.
- timeit turns off garbage collection while timing, which makes independent timings more comparable. Re-enable it with
gc.enable()as the first statement in setup if GC matters. - Other programs on the same machine can affect
default_timer()measurements, so repeat the timing and use the best time. - With
repeat(), themin()of the result is probably the only number you should be interested in.
See also
Claims
- id: C1 claim: timeit avoids common traps for measuring execution times. source: S1 evidence: "It avoids a number of common traps for measuring execution times."
- id: C2 claim: Setup time is excluded from the timed run. source: S1 evidence: "The execution time of setup is excluded from the overall timed execution run."
- id: C3 claim: The repeat default is 5. source: S1 evidence: "how many times to repeat the timer (default 5)"
- id: C4 claim: timeit measures execution time of small code snippets. source: S2 evidence: "Tool for measuring execution time of small code snippets."
Sources
- [S1] timeit — Measure execution time of small code snippets. Python docs. https://docs.python.org/3/library/timeit.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/timeit.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/timeit.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, timeit — Measure execution time of small code snippets; python/cpython (GitHub), Lib/timeit.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, timeit — Measure execution time of small code snippets; python/cpython (GitHub), Lib/timeit.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python timeit times small snippets from the command line or code, avoiding common timing traps." iinventedlight Library, iil:note:6c5nna7x, version of 2026-11-18. https://iinventedlight.com/library/notes/python-timeit/. License: CC-BY-4.0.
@misc{iil_note_6c5nna7x,
author = {{iinventedlight}},
title = {{Python timeit times small snippets from the command line or code, avoiding common timing traps}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-timeit/},
note = {Permanent ID iil:note:6c5nna7x. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-timeit.table.json (table; CC-BY-4.0)
- python-timeit.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-timeit.md.