Sub topic 13
When an amortised bound is not enough
This chapter assumes you know the aggregate, accounting and potential methods. It also assumes you know how a doubling dynamic array and a resizing chained hash table work, and the difference between expected and amortised cost.
What an amortised bound promises
An amortised bound limits the total cost of any sequence of n operations. It says nothing about the cost of one operation. A doubling array has O(1) amortised append, yet one append can copy every element. The bound holds for the whole sequence and does not hold for a single call.
The program below measures both numbers for a doubling array.
def append_costs(n):
"""Cost of each append: 1 per write plus 1 per element copied on resize."""
size, capacity = 0, 1
costs = []
for _ in range(n):
cost = 1
if size == capacity:
cost += size # copy every element into the new array
capacity *= 2
size += 1
costs.append(cost)
return costs
costs = append_costs(100000)
print("average:", sum(costs) / len(costs))
print("worst single append:", max(costs))
assert max(costs) > 1000The average stays under 3, but the worst single append costs 65537 units. Both facts are true at once. The amortised bound only speaks about the first one.
Where the gap hurts
The gap matters when a program must meet a limit on each individual operation.
- Latency-sensitive code. A game loop with a 16 ms frame budget, an audio callback, or a request handler with a tail-latency target (such as the 99.9th percentile) fails when one rare operation stalls. The average looks fine while the worst case misses the deadline.
- Real-time systems. Hard deadlines need a worst-case bound per operation. An amortised bound gives no such guarantee.
- Shared locks. A resize that runs while holding a lock blocks every other thread for the whole copy.
- Persistent or shared structures. Amortised proofs assume the cost is paid by the operations that came before. If an old version of a structure is reused many times, the same expensive operation can repeat. A structure that is amortised O(1) when used in one sequence can be O(n) per operation when several callers branch from the same expensive state.
- Adversarial input. An attacker who controls the sequence cannot beat an amortised bound. But a hash table with a predictable hash function can still be driven into long chains. The expected-cost argument fails there, even though the resizing argument still holds.
Ways to restore a worst-case bound
You have three main options.
| Option | How it works | Cost |
|---|---|---|
| Incremental resizing | Keep old and new tables. Move a few entries on every operation. | More complex code. Lookups may check two tables. |
| Pre-allocation | Reserve the final capacity up front, for example from a known size. | Wasted memory if the estimate is too high. |
| Move the work elsewhere | Resize in a background thread, or at a quiet moment such as between frames. | Needs synchronisation and a plan for when the table fills first. |
Incremental resizing turns an O(n) pause into O(1) work per operation, at the price of extra space and extra bookkeeping. This trades an amortised bound for a worst-case one.
Common errors
- Quoting "O(1) append" in a latency discussion without saying it is amortised.
- Confusing amortised with expected. Amortised bounds hold for every input sequence. Expected bounds depend on a random hash function. A hash table insertion needs both claims.
- Averaging over too few operations. The bound only applies to a whole sequence that starts from an empty structure, so a short window can sit entirely inside an expensive stretch.
Exercise
A service appends to a Python-style dynamic array inside a request handler. Average append time is 50 ns, but 1 request in 100000 is 20 ms slower than the rest. What is the likely cause, and name one fix that gives a worst-case bound?
Show answer
The likely cause is a resize that copies the whole array, which the amortised bound allows. The array has to copy about as many elements as it holds, so the slow request pays for that copy. One fix is to pre-allocate capacity for the expected size. Another is to grow the structure incrementally so each append moves only a few elements.