timeit, doctest and ‘batteries included’

Measure before rewrite. doctest keeps examples in the docstring. The stdlib is large on purpose — look it up before you pip a one-liner.

Charts from numbers

A picture that a principal can read in five seconds.

You will be able to

  • Measure a snippet before rewriting it
  • Keep examples next to the spec (doctest energy) and lock them with PASS/FAIL
  • Look in the stdlib before you pip a one-liner

Batteries included means look it up

timeit puts a number on ‘slow’. doctest keeps a tiny example in the docstring so the next intern can run it. unittest and pytest are the heavier runners. The stdlib is large on purpose — csv, json, pathlib, statistics — look it up before you pip a package that is twelve lines of what you already have.

This sandbox cannot timeit or collect tests. You still write a correct total, print PASS, and count rows you would measure. Copy the laptop sample for timeit and a pytest-shaped assert.

Quality is cheaper than clever

A doctest that says percent(9, 10) is 90 is the same contract as check(got, want). If the intern ‘optimises’ with threads and the total drifts, FAIL must still fire. Measure only after PASS.

Words that matter

timeit
Stdlib helper to measure a snippet.
doctest
Examples in the docstring that can be run as tests.
Batteries included
Look in the stdlib before you pip a one-liner.

Common mistakes

Avoid: pip installing a helper that is already in the stdlib.

Do this: Look it up; then pin only what you still need.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — spec as PASS/FAIL

doctest energy: 9 of 10 is 90.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — count work you would timeit

A row counter is the start of a timing story.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

On a full Python install — timeit, pytest (and doctest energy)

Number on a slow percent; lock 9/10 → 90. Stdlib timeit; pip pytest in the project venv.

timeit: stdlib. pytest: python -m pip install pytest (in the project venv)

timeit the formula. pytest (or doctest) locks the spec.

Real library code (not run in this browser sandbox)

from timeit import timeit

def percent(used, seats):
    """Utilisation. Example: percent(9, 10) == 90."""
    return used * 100 / seats

print("timeit", timeit(lambda: percent(9, 10), number=10000))

# pytest in the venv:
# def test_nine_of_ten():
#     assert percent(9, 10) == 90

Example program — check() is a tiny pytest

got vs want. Batteries: you already know print and if.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Lock 90

percent(used, seats) returns used * 100 / seats. Print percent(9, 10) and PASS.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. timeit is for…
2. ‘Batteries included’ means…

Progress is stored in a browser cookie on this device.