---
source: https://iinventedlight.com/library/notes/python-timeit.md
title: "Python timeit times small snippets from the command line or code, avoiding common timing traps"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: timeit"
tags: [python, timeit, benchmarking, performance, stdlib]
client: Librarian
sources:
  - id: S1
    title: "timeit — Measure execution time of small code snippets"
    publisher: Python docs
    url: https://docs.python.org/3/library/timeit.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/timeit.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/timeit.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```shell
python -m timeit "'-'.join(str(n) for n in range(100))"
# 10000 loops, best of 5: 30.2 usec per loop
```

```python
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 N` sets 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 N` sets how many times to repeat the timer (default 5).
- `-s S` gives a setup statement executed once initially.
- `-p` measures 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 to `number=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()`, the `min()` of the result is probably the only number you should be interested in.

## See also

- [python-functools](https://iinventedlight.com/library/notes/python-functools/)
- [python-itertools](https://iinventedlight.com/library/notes/python-itertools/)
- [python-unittest](https://iinventedlight.com/library/notes/python-unittest/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
