Basis Lab

How Long It Takes

Why the Constants Get Dropped

Last timeThe Shape of the Curve

Throwing away constant factors and smaller terms is what makes the comparison portable, and it is also what makes it occasionally wrong. Both halves of that are worth holding.

The last lesson placed procedures on a ladder of shapes and ignored everything else about them. This lesson says why that is allowed, and then says where it stops being allowed, because both halves get used.

FIG 1The claim of order n squared, checked against a real cost at four sizes
stepitemswhat the procedure really costsn squared, the shape claimed for itmultiples of n squared needed to cover thatwhat happened
1567.5 steps, being 12.5 and 15 and 40252.7 timesAt five items the real cost is nearly three times the shape claimed for it, so a ceiling of one n squared is simply false here. Every growth claim therefore comes with a size below which it is not being made at all.
2501,440 steps, being 1,250 and 150 and 402,5000.58 timesOne size up and the multiple has fallen below one. From here on, 0.58 of an n squared covers this procedure at every size there will ever be, which is the whole of what the claim promises.
31,000503,040 steps, being 500,000 and 3,000 and 401,000,0000.503 timesThe multiple is still falling and it is clearly going somewhere. Where it is going is the coefficient on the leading term, because the other two terms are on their way to nothing.
41,000,000500,003,000,040 steps, being 500,000,000,000 and 3,000,000 and 401,000,000,000,0000.5 timesHalf an n squared, to five places. The claim was true from fifty items onward with one fixed multiple, and that is the only thing it was ever asserting.
4 steps
The last column is the thing a growth claim will not tell you. There is some multiple that covers the cost, it is never stated, and the promise is only that it stops rising: at five items you would need 2.7 of them, and from fifty items onward 0.58 covers every size for ever. Both of those come back later in this lesson, the multiple in the next section and the starting size in the one after it. Note also that this is a ceiling and not a description. A procedure that scans a list once satisfies a ceiling of n squared perfectly truthfully, which is why somebody saying their code is order n squared may be making a weak claim rather than a sad one, and when the shape is meant exactly the word tight is the usual signal.
FIG 2A bad shape with a small constant against a good shape with a large one
0.0020000.0040000.0060000.0080000.001.063.3125.5187.8250.0input size
100n, the better shapen squared, the worse shape
Below a hundred items the quadratic procedure is genuinely the faster one, by as much as a hundredfold at the left edge of the plot. At a hundred they are equal. Beyond it the linear procedure pulls away and never looks back: at ten thousand items it is a hundred times faster and at a million it is ten thousand times faster. The constant decides who wins early and the shape decides who wins eventually, and since the input is the thing that grows, eventually is usually the case worth planning for. This is the whole argument for the convention, and it is also the picture to keep in mind when the convention misleads.

Why the constant goes

A factor of a hundred is not small, and the plot still ends with the linear procedure far ahead. That is the point. A constant shifts the crossing; it cannot remove it.

It is also the wrong thing to attribute to the procedure in the first place. The same algorithm in C and in Python differs by a factor of around fifty, and nobody calls those two different algorithms.

FIG 3The share each term holds as the input grows
n squared over 23n40largest term as a share of the total
n = 512.5015.0040.000.19
n = 501250.00150.0040.000.87
n = 1000500000.003000.0040.000.99
n = 1000000500000000000.003000000.0040.001.00
The two marked cells are the argument. At five items the leading term is under a fifth of the work and dropping the rest would be absurd; at a million it is everything, and carrying the rest would add length to the expression without changing any comparison made with it. The test for dropping a term is whether its share tends to zero, which is a statement about where this table is going rather than about any row of it. Notice that the first row is also a warning: the convention is about large inputs and says so, and the next section is about people who forget that.

Where this goes wrong

Three situations, all common.

The input never grows. A list that is always under fifty items has no large case, so the shapes do not matter and the constants are the entire story. A thousand such lists per second makes the constants matter even more.

The constant is not small. Some procedures with better shapes carry setup costs so large that no realistic input reaches the crossing point. Several matrix multiplication methods with better exponents than the schoolbook one are never used for this reason: the input sizes where they would win do not fit in any machine.

The shapes are equal. If two procedures are both linear, the growth statement is silent by construction, and the only remaining instrument is measurement.

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 Droppedyou are here
  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