14/38
Part 3 of 7 · Your own stackLearn and do
Recursion was faster when it fit, and only your stack got through a chain
Both are worth knowing, so measure them.
| Tree, 100,000 regions | Recursive all_sizes | sizes_with_stack |
|---|---|---|
| Random, each region under an earlier one, height 23 | 37.5 ms | 54.3 ms |
| A chain, height 99,999 | RecursionError | 17.1 ms |
Medians of seven runs, Python 3.12, on the machine these numbers come from. When recursion fits, it's the faster of the two here, and the stack version spends a second pass on the stored order. When it doesn't fit, nothing else matters.
Both are time. The memory differs in kind, and the two shapes that pull it apart are the chain and the star, one top holding everyone else:
A star: one top over 99,999 regions. At its peak, how many regions sit on your stack?
Pick an answer above to see the rest