Basis Lab

How Long It Takes

Space Is a Cost As Well

Last timeBest Case, Worst Case, and the One You Get

Memory grows with the input too, it runs out more abruptly than time does, and it can be traded against time deliberately in both directions.

Everything so far has counted operations. A procedure also occupies memory,

that occupation grows with the input in exactly the same way, and it fails

differently when it runs out.

FIG 1Time and memory for the same four tasks
timeextra memoryfails howat a million items
sum a list3111
sort a list in place4111
sort by building a new l4322
count co-occurring pairs5523
The first two columns use the ladder codes from earlier, one to five. The third column codes how the procedure fails: one for gets slower, two for runs out of memory, and the fourth column codes the outcome at a million items as fine, tight, or impossible. The two marked cells are the reason this lesson exists. A quadratic time procedure at a million items is merely a procedure that will not finish this week; a quadratic memory procedure at a million items cannot be started on any machine, because a million million entries is not a wait, it is a wall.

Memory fails abruptly

Time degrades. A procedure that takes twice as long takes twice as long, and

somebody notices and complains.

Memory does not degrade. A procedure that needs slightly more memory than

exists does not run slightly worse; it slows by a factor of thousands as the

machine starts swapping, or it stops with an error. There is no gentle region

before the cliff, which is why memory is worth counting before the input gets

large rather than after.

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 Workopening only
  5. 05Best Case, Worst Case, and the One You Getopening only
  6. 06Space Is a Cost As Wellyou are here
  7. 07Occasionally Expensive, Usually Freeopening only
  8. 08When the Analysis Liesopening only

Read alongside