The doubling test
After thiswhat you will be able to doUse T(2n)/T(n) to diagnose a likely growth shape, and recognise when lower-order terms make the diagnosis premature.
Questionwhat this lesson answersEach growth shape answers a doubled input in its own way: constant stays, linear doubles, quadratic quadruples. Run that backwards, measuring how the cost responds when you double the input and reading off the shape you were not told, and you have a diagnostic. How does it work and where does it mislead?
Not coveredwhat this lesson leaves outWe read a growth shape off the ratio of costs at n and 2n for the shapes this arc has met. We do not handle noisy real measurements, and we do not prove that a measured ratio pins the shape.
The cost lesson gave each piece of work a response to a doubled input. Work done once stayed put. Work done once for each input item doubled. Work that paired each item with every item became four times as large because both directions doubled. Those responses can be used in reverse. If the shape of a cost is hidden, measure it at one input size and then at twice that size. The way the two measurements compare is evidence about the arrangement of the work.
Start with the comparison before its name. Let be the cost at an input of items. Make the input items and divide the second cost by the first. The resulting number says how many copies of the first cost fit into the second. That number is called the doubling ratio:
The response has a clean signature when one shape is the whole cost. A cost that stays fixed gives one. A cost that keeps pace with its input gives two. A cost made by multiplying the input by itself gives four. If the input is multiplied by itself three times, there are three independent input-sized choices. That shape is called cubic, and its response is eight. One more arrangement doubles the cost itself each time a single item is added, so doubling the input multiplies the cost many times over and its ratio grows rather than settling. That shape is called exponential.
Here are the signatures the arc has met. The input-times-halving-count row is the shape often described as one logarithmic count for each input item. It has a ratio a little above two because doubling both doubles the input part and adds one to the halving count.
The entries are arithmetic, not labels to memorise. For a cost built by multiplying by itself a fixed number of times, doubling every copy multiplies the cost by two raised to that same number. Squaring gives four and cubing gives eight. For the input-times-halving-count shape, the extra halving is why the ratio is just above two, then slowly approaches it. For a logarithmic cost, the ratio is a less useful clue because both values are close together. Its difference is the clearer clue: doubling adds exactly one halving. For the exponential cost where , the calculation is
The ratio itself grows with , which separates this shape from every fixed multiplier in the table.
The method is now short. Measure the cost at and at . Form their quotient. A value near four suggests the input was paired with itself. A value near two suggests one pass through the input. A value near one could mean fixed work or a logarithm, so check the difference too. If the difference stays one while the ratio drifts toward one, repeated halving is the better guess. If the ratio itself keeps climbing, an exponential response is the warning sign.
A clean quadratic answer
Start with a quadratic cost at sixteen input items. Double the input once. The second cost is four times the first because both input-sized directions doubled.
Measure, then compare
Read the cost at one input and its double
Pick a cost shape, then compare the two bars. The quotient is the clue the doubling test gives you, not a replacement for counting the algorithm.
- Cost at 16 inputs256
- Cost at 32 inputs1,024
- Input items
- 16
- Cost at input
- 256
- Cost at doubled input
- 1,024
- Doubling ratio
- 4
- Halving-count difference
- 1
Quadratic cost at 16 input items is 256. At 32 input items it is 1,024. The doubling ratio is 4. the cost pairs the input with itself.
The running polynomial
Watch a ratio climb toward the quadratic signature
This view keeps the full cost from the earlier lessons. Its line is a set of sampled doubling ratios. The dot marks the input selected below.
- Polynomial input
- 10
- Cost at input
- 25,300
- Cost at doubled input
- 31,200
- Polynomial ratio
- 1.233
At small inputs the ratio is not a clean classification. The constant and one-pass pieces still occupy a visible part of the total, so the ratio has not yet reached four.
That word “suggests” matters. The ratio is clean only after one piece of a cost has become the visible part of the total. Return to the running polynomial from the arc. It has the long-run shape of the input multiplied by itself, but at a small input the fixed setup and one-pass work are still present:
The ratio is well below four, not because the eventual shape changed, but because the other pieces have not yet become small beside the piece that grows like the input times itself. Those slower pieces are called lower-order terms. They contaminate a small measurement by mixing their own doubling responses into the quotient. The cost’s long-run shape has not failed. The measurement is asking too early.
As the input grows, the ratio climbs toward the clean quadratic response:
The approach is what the n zero threshold from the What O(f) actually is door was built to name. Before that threshold, the larger piece has no promise to dominate. The When the constants come back door gave the same finite-size fact a practical form: the pieces we later discard can decide what a program looks like at the sizes it actually runs.
The doubling test is therefore a way to form a quick guess from two measurements, not a proof. It works best after the input is large enough for one response to dominate. A clean ratio can point you toward the right count. It cannot replace doing that count, and a dirty ratio may only mean that the input has not reached the part of the curve where the intended shape shows itself.
Doorswhat to read next, and why
- What a cost is made ofThe responses to doubling (stay, double, quadruple) were built there, one shape at a time. This lesson runs them backwards.
- Logarithms, and why the base stops matteringA logarithmic cost adds a constant when the input doubles rather than multiplying by one. That signature comes from the halving count in that lesson.
- What O(f) actually isThe doubling ratio only settles to its clean value once the input passes the threshold where lower-order terms stop contaminating it. That threshold is the n-zero from the O(f) definition.
- When the constants come backAt small inputs the ratio is pulled off its clean value by the lower-order terms, the same finite-size effect that lesson is about.
Symbolswhat each one means, and whether we defined it, measured it, or just started there
- nStatus: defined
- the input size, doubled to 2n to run the test
- T(n)Status: defined
- the measured or modelled cost at input size n
- the doubling ratioStatus: defined
- T(2n) divided by T(n), the number this test reads a growth shape off
What these classifications mean
- defined
- circular by construction, true because we chose it
- empirical
- a measured claim about the world that could have come out otherwise
- bottoms out
- a primitive of the model, with nothing under it here
- door
- used here, explained elsewhere