Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 01 · Portfolio Projects · Build It Yourself · about 17 min to read

Play it as a journeyTutor comes with a plan

P11: Make it 10x faster, with proof

S1 - Build Your Own Database
S1 - Build Your Own Database · Open board

The board shows a database taking in rows and reading them back. The program in this lesson reads a file of rows too, and the work is to find which part of that reading is slow.

Asha has a Python script that reads her company’s web server log and prints a daily report. Last month it took a few seconds. This month the log is bigger and the script takes much longer, so the report is late every morning. She has a free afternoon. She has one wrong idea about what to do with it: open the file and start rewriting the parts that look slow.

This lesson uses a made-up log of 300,000 lines and a script I wrote for it. Asha and the log are teaching values. Every time in this lesson is measured on one machine, a 16-core Linux computer running Python 3.12.3, on 28 November 2026. Your times will differ. The ratios between them will be close.

By the end of this lesson you will be able to take a slow program, find its slowest part by measuring, fix that one part, measure again, and show a before-and-after table that proves how much faster it got.

The history (optional)

Donald Knuth wrote in “Structured Programming with go to Statements” (Computing Surveys, December 1974) that programmers should forget small efficiencies “about 97% of the time”, and that “premature optimization is the root of all evil”. The next sentence says not to pass up the critical 3%. Finding the 3% needs measurement, which is this lesson.

Before you start. You need Watching a live machine: perf, strace and flame graphs, which showed a sampling profile: a program is stopped many times a second and the chain of function calls is written down each time. You need P6: A model you can defend, where a baseline is the first score you put next to every later score. You need A repo a stranger can judge in two minutes, which says a project page should show measured results. This lesson gives you the measured result for speed.

Lesson 18 of 19 in Portfolio Projects

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The program, and the first number: a benchmark
  • Profile: find where the time goes, not where it looks like it goes
  • One bottleneck at a time: a bottleneck is the part that sets the total
  • The small fixes: measure them too, and keep only what you can show
  • Making the input
  • The slow version, v0
  • The four changes, as the exact differences
  • The benchmark script
  • A small sampling profiler that prints a flame-style view
  • Python’s own profiler gives a different picture
  • The limit on speedup: Amdahl’s law
  • Does the speedup hold on bigger input?
  • Edge cases and trade-offs

Terms it explains: benchmark, baseline, profile, bottleneck, speedup, set, compaction, log-structured, offset, WAL.

Review Portfolio Projects