Skip to the workspace
Pebellslearn by building
Analysis II: Recurrences, Amortisation & Randomness  ›  Capstone Project

Capstone — the Tunebox Cost Lab

45 minMeasure what code really costs, fit a curve to the counts, and check it against the bound you proved

dsa / Capstone — the Tunebox Cost Lab

Explore each scene at your own pace.

01 · Step 1: instrument

Step 1: instrument

Capstone — the Tunebox Cost Lab

Definition
Empirical complexity measurement counts an algorithm's dominant operation at doubling input sizes, then reads the growth rate from the ratios between counts or from the slope of a log-log plot.
In plain words
Instead of guessing how fast something grows, you put a counter in it and run it on bigger and bigger inputs. If doubling the input doubles the count it is linear; if it quadruples the count it is quadratic.

In short: instrument a function with a counter on its dominant operation, measure it at sizes that double, and the growth rate falls out of the ratio between neighbouring counts (×2 linear, ×4 quadratic). For a precise exponent, take logs of both n and the count: on a log-log plot a power law a·n^b becomes a straight line whose slope is b. The lab then checks that slope against the bound you proved on paper — and settles the n log n vs n^1.1 ambiguity by dividing the counts by each candidate model and seeing which one leaves a flat line.

A log-log plot of four measured points on a straight fitted line; the slope, read as rise over run, is the exponent b in c ≈ a·n^b

You now have two ways of knowing what code costs. On paper: read a recurrence off the code, solve it with a recursion tree, account for spikes with a bank of tokens, average over random pivots. In the machine: count the operations while the code runs. Each one checks the other. A proof with a slip in it disagrees with the count, and a count taken on an unlucky input disagrees with the proof.

The capstone puts them together in one instrument. The cost lab takes a function from sections 1 to 6, runs it on inputs of growing size, counts its real operations, fits a curve to the counts, and reports the exponent. You then compare that exponent with the bound you derived.

Step 1: instrument

Timing with a stopwatch measures your laptop. Counting measures the code. Pick the operation that dominates, the one inside the innermost loop, and add a counter there. For insertion sort from section 3 that is the shift:

python

A reversed list of four needs 6 shifts, a sorted one needs none. Which input you measure on matters as much as how you count, so the lab always states it: worst case (reversed), best case (sorted), or a seeded random input for the average.

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — the cost lab
This piece — fit_exponent(): Fits a power law to measured counts and reports the exponent.
Scenario Before Tunebox launches in a new country with ten times the listeners, the team runs every feature through the cost lab: measure, fit, compare with the proof, and find which feature breaks first.
fit_exponent.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
fit_exponent(ns, counts) → floatThe fitted exponent b in count ≈ a·n^b, rounded to 2 places.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.