|
| 1 | +# Python Performance Lab: Sharpening Your Instincts |
| 2 | + |
| 3 | +A PyCon US 2026 hands-on tutorial. You optimize intentionally slow Python code |
| 4 | +across three rounds plus a team challenge, measuring every change with |
| 5 | +[CodSpeed](https://codspeed.io). |
| 6 | + |
| 7 | +## Rounds |
| 8 | + |
| 9 | +| Round | Topic | Skills | |
| 10 | +| -------------------------- | -------------------- | ------------------------------------- | |
| 11 | +| [1](rounds/1_histogram/) | Byte-pair histogram | Data representation, vectorization | |
| 12 | +| [2](rounds/2_corruption/) | Corruption scanner | Vectorization, parallelism | |
| 13 | +| [3](rounds/3_dna/) (final) | DNA sequence matcher | Everything above, as a team challenge | |
| 14 | + |
| 15 | +Each round ships an intentionally slow `baseline.py` (a read-only reference), |
| 16 | +a `solution.py` you edit, deterministic data generators, parametrized |
| 17 | +correctness tests, and benchmarks that run baseline and solution |
| 18 | +side-by-side. |
| 19 | + |
| 20 | +## Setup |
| 21 | + |
| 22 | +You need [`uv`](https://docs.astral.sh/uv/). Python 3.15t will be downloaded directly. |
| 23 | + |
| 24 | +The order below matters: forking, logging in, and doing the first run on `main` |
| 25 | +register you on the live leaderboard, so every later push to your branch shows |
| 26 | +up as a side-by-side comparison against your own baseline. |
| 27 | + |
| 28 | +```bash |
| 29 | +# 1. Fork github.com/CodSpeedHQ/pyconus-2026-tutorial, then clone your fork. |
| 30 | +git clone https://github.com/<you>/pyconus-2026-tutorial && cd pyconus-2026-tutorial |
| 31 | + |
| 32 | +# 2. Install deps + generate the datasets (~650 MB total). |
| 33 | +uv sync |
| 34 | +uv run scripts/setup.py |
| 35 | + |
| 36 | +# 3. Install the CodSpeed CLI and log in. |
| 37 | +curl -L https://codspeed.io/install.sh | sh |
| 38 | +codspeed auth login |
| 39 | + |
| 40 | +# 4. Branch off. Every push to this branch re-runs and re-ranks you. |
| 41 | +git checkout -b <your-name> |
| 42 | +``` |
| 43 | + |
| 44 | +Generate smaller datasets on lower-spec machines: |
| 45 | + |
| 46 | +```bash |
| 47 | +uv run scripts/setup.py --round1-mb 10 --round2-mb 32 --round3-mb 100 |
| 48 | +``` |
| 49 | + |
| 50 | +## Working on a round |
| 51 | + |
| 52 | +Every round directory ships its own `README.md`. The commands are the same |
| 53 | +shape every time, illustrated here for Round 1: |
| 54 | + |
| 55 | +```bash |
| 56 | +# Correctness tests against the small fixture. |
| 57 | +uv run pytest rounds/1_histogram/ |
| 58 | + |
| 59 | +# Walltime benchmark against the full dataset. |
| 60 | +uv run pytest --codspeed rounds/1_histogram/ |
| 61 | + |
| 62 | +# Same, run through the CodSpeed CLI with the walltime mode |
| 63 | +codspeed run --mode walltime -- uv run pytest --codspeed rounds/1_histogram/ |
| 64 | +``` |
| 65 | + |
| 66 | +Edit `solution.py` to optimize. Leave `baseline.py` alone so the side-by-side |
| 67 | +comparison stays meaningful. Every test and benchmark is parametrized over |
| 68 | +both implementations, so the output always shows `[baseline]` versus |
| 69 | +`[solution]`. |
| 70 | + |
| 71 | +## Layout |
| 72 | + |
| 73 | +``` |
| 74 | +rounds/ |
| 75 | + 1_histogram/ # baseline.py, solution.py, gen_data.py, tests. |
| 76 | + 2_corruption/ |
| 77 | + 3_dna/ |
| 78 | +scripts/ |
| 79 | + setup.py # one-shot data generation across every round. |
| 80 | +``` |
| 81 | + |
| 82 | +Each round's `data/` directory is generated locally and gitignored. |
0 commit comments