Basis Lab

How Long It Takes

Reading the Cost Off the Work

Last timeWhy the Constants Get Dropped

Three patterns cover most of it: work done in sequence, work nested inside work, and work that halves what is left. Recognising them turns analysis into reading.

The previous lessons established the ladder and the convention. This one is

the working skill: looking at a piece of work and saying what shape it has.

Nearly all of it reduces to three patterns.

FIG 1The three patterns and what they produce
repetitionscost of the bodythe totaltypical example
one pass over the input1111
a pass inside a pass2122
halving what is left3133
a pass, then a pass1114
The numbers here are codes rather than measurements, and the table is a key rather than an argument: row by row the totals are linear, quadratic, logarithmic and linear again. The two marked cells are the two patterns that have to be learned, since the first and last are what everyone already assumes. The rest of this lesson takes them in turn, and the fourth row is the one worth noticing early, because a linear step added to a quadratic one changes nothing and is optimised anyway with depressing regularity.

Sequence and nesting

Two loops one after the other add. By the previous lesson the sum keeps only

its larger part, so a linear pass bolted onto a quadratic procedure is free,

and speeding that pass up gains nothing at all.

Two loops one inside the other multiply, because the inner body runs once per

pass of the outer loop. That is the entire origin of quadratic cost.

FIG 2Reading four fragments by their repetition structure
stepthe workouter repeatsinner work per passtotal
1one loop over n itemsnconstantn
2a loop over n, then another over nn then nconstantn, the sum reduced
3a loop over n inside a loop over nnnn squared
4a loop over rows inside a loop over columnsrcr times c
5a loop over n containing a list membership testnn, hiddenn squared
6a loop over n containing a dictionary lookupnconstantn
6 steps
The last two rows are the same code to a careless reader and differ by an entire rung of the ladder. A membership test against a list scans the list, so it is a loop that was not written as one; the same test against a dictionary or a set is a constant time lookup. The fourth row is worth its own note: a procedure over a grid is linear in the number of cells and only quadratic when the grid is square, and calling it quadratic without saying in what is how two correct people end up disagreeing.

Halving

The third pattern is the one that has to be taught, because nothing in daily

experience prepares anyone for how few steps it takes.

The lesson stops here

1 more paragraph to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Counting Steps, Not Seconds
  2. 02The Shape of the Curveopening only
  3. 03Why the Constants Get Droppedopening only
  4. 04Reading the Cost Off the Workyou are here
  5. 05Best Case, Worst Case, and the One You Getopening only
  6. 06Space Is a Cost As Wellopening only
  7. 07Occasionally Expensive, Usually Freeopening only
  8. 08When the Analysis Liesopening only

Read alongside