n = items10new listn items1items[5]2bisect(items, x)3x in items4sorted(items)5for a in items:6 for b in items: c += 17ages["Sam"]8items.insert(0, x)11,0001 million1 billion1 trillionsteps · each line is ×10O(1)O(log n)O(n)O(n log n)O(n²)
Code card: one line of Python per growth rateBars: steps taken for n items, on a ×10 scaleCounters: the exact step count under each barn block: how many items the code works onEach bar counts the steps one kind of code takes for n items. As n steps up from 10 to 1,000 to 1,000,000, the bars glide to their new counts and the counters crossfade. The O(1) bar never moves; O(log n) creeps to about 20; O(n) follows n; O(n log n) reaches about 20 million; O(n²) shoots up to a trillion. The bars stand against a log scale: each line on the back wall is 10 times the one below it. On an ordinary scale the trillion bar would be a million times taller than the million bar.

Counting steps

  1. A stopwatch gives different times on different computers. So we count steps instead: simple actions, like looking at one item. Big O asks how that count grows as n, the number of items, grows from 10 to a million.

Five growth rates

The rules

In practice

Try it in the Python prompt

;; step 1/11 · Count steps, not seconds
>>> steps = 0
>>> for x in range(1_000):
...     steps += 1      # one step per item
...
>>> steps
1000

The bars stand against a log scale: each line on the back wall is 10 times the one below it. On an ordinary scale the trillion bar would be a million times taller than the million bar. The O(log n) bar counts the most halvings binary search can need on n items, which is log₂(n + 1) rounded up: 4 for 10 items, 10 for 1,000 and 20 for 1,000,000. bisect comes from the standard library: from bisect import bisect.