Why appending stays fast
After this: Say what amortized O(1) means for appends and why it holds.
On this page · 6 sections · 3 checks
You saw that an append into a full row copies every value. That one append is O(n). Yet people say that appending is fast. How can both be true?
The answer is that the expensive appends are rare. This lesson counts them.
Think of a yearly fee
Imagine a gym that charges 365 once a year, and nothing on other days. On one day you pay a lot. But if you spread the fee over the whole year, it comes to 1 per day. Appending works in a similar way: a few appends are expensive, and when you spread their cost over all the appends, the cost per append is small.
Count the copies
Start with a row of one cell and append sixteen values. The row grows only when it is full, and each time it copies the values it holds.
| The row is full at size | Values copied |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 4 | 4 |
| 8 | 8 |
| Total | 15 |
Sixteen appends caused fifteen copies. The next append is expensive: the seventeenth value finds the row of 16 full, so it copies 16 values, and the total jumps to 31. So the total comes in jumps. But it never gets far ahead. Each doubling copies one more value than all the earlier doublings put together, and this keeps the total below 2n, twice the number of values.
The average stays small
Now add up the work for n appends.
- Each append writes its own value once. That is n writes.
- The copies add fewer than 2n more writes.
The total is less than 3n writes, so the average is less than 3 writes per append. For the sixteen appends above it is about 2. That average does not grow, however many values you add.
The name for this is . "Amortized" means averaged over many operations. It says that one append may be expensive, but the cost per append over a long run is constant.
a = DynamicArray()a.capacity = 1a.cells = [None]for v in range(1, 17): # sixteen appends from a single cell a.append(v)copies = a.writes - a.size # every write that was not the append itselfprint(a.capacity, copies, round(a.writes / a.size, 2))16 15 1.94
DynamicArray a = new DynamicArray();a.capacity = 1;a.cells = new int[1];for (int v = 1; v <= 16; v++) a.append(v); // sixteen appends from a single cellint copies = a.writes - a.size; // every write that was not the append itselfSystem.out.println(a.capacity + " " + copies + " " + String.format("%.2f", (double) a.writes / a.size));16 15 1.94
Watch it happen
Each append shows its own count in the copies counter, and the counter starts again at the next append. Add the counts up as you go, and compare the total with the number of values.
- Append 1The row has one cell, so this value fits.
- Append 2
- Append 3
- Append 4
- Append 5
- 0·
Run an operation to begin.
Check yourself
Appending to a dynamic array is called amortized O(1). What does that mean?
Growing from capacity 1 by doubling, about how many copies in total do n appends cause?
This code makes sixteen appends, starting from a single cell. What does it print (capacity, copies, writes per value)?
a = DynamicArray()a.capacity = 1a.cells = [None]for v in range(1, 17): # sixteen appends from a single cell a.append(v)copies = a.writes - a.size # every write that was not the append itselfprint(a.capacity, copies, round(a.writes / a.size, 2))DynamicArray a = new DynamicArray();a.capacity = 1;a.cells = new int[1];for (int v = 1; v <= 16; v++) a.append(v); // sixteen appends from a single cellint copies = a.writes - a.size; // every write that was not the append itselfSystem.out.println(a.capacity + " " + copies + " " + String.format("%.2f", (double) a.writes / a.size));Connect
- Say that growing copies every value, but happens only when the row is full.
- Explain amortized O(1): the total number of copies stays under 2n, so the average cost of an append does not grow.