Basis Lab

How Long It Takes

The Shape of the Curve

Last timeCounting Steps, Not Seconds

Almost all work falls into a handful of growth shapes. Knowing them in order, and knowing what each does when the input doubles, is most of the practical value of the subject.

The previous lesson produced expressions such as 3n+73n + 7 and

n(n−1)2\frac{n(n-1)}{2}. This one throws away almost all of that detail and keeps

the part that decides things.

FIG 1
the shapen = 100n = 10,000n = 1,000,000doubling n multiplies the cost by
constant111nothing: it is the same
logarithmic71320nothing: it adds one step
linear10010,0001,000,0002
linear times logarithmic664132,87719,931,569a little over 2
quadratic10,000100,000,0001,000,000,000,0004
exponentiala 31-digit numbera 3,011-digit numbera 301,031-digit numberit squares it

The ladder

The last column is the one to memorise, because it compresses the whole table

into six answers. The quadratic row is where usable stops: a million rows

compared pairwise is a million million operations, which is hours on a machine

that does a billion a second. The exponential row is not a performance problem

at all. At a hundred items its count already has thirty one digits, and no

machine that will ever be built finishes it, which is why the honest word for

exponential work is impossible rather than slow. Notice also how little

separates the top three rows at any size, and how little that separation

matters next to the gap below them.

FIG 2The middle of the ladder, where the real decisions are
0.001250.002500.003750.005000.002.017.533.048.564.0input size
logarithmiclinearlinear times logarithmicquadratic
The exponential row is left out because it leaves the top of any plot almost immediately, and the remaining four are the ones real design choices actually sit between. Two things are visible here that the table hides. The linear and the linear times logarithmic curves stay close over this whole range, which is why a sort is rarely the thing to optimise away. And the quadratic curve is not obviously bad on the left of the plot, which is exactly why quadratic code reaches production: on a developer sample of fifty rows it looks fine.

What doubling does

The most useful sentence about each shape concerns what happens when the

input doubles. Constant stays put. Logarithmic gains one step. Linear

doubles. Linear times logarithmic a little more than doubles. Quadratic

quadruples. Cubic multiplies by eight. Exponential squares.

That last one deserves its own line. Squaring is not a slowdown, it is a wall:

a procedure taking a second at forty items takes about three hundred years at

eighty.

The lesson stops here

3 more paragraphs 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 Curveyou are here
  3. 03Why the Constants Get Droppedopening only
  4. 04Reading the Cost Off the Workopening only
  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