Big-O and Algorithm Complexity
Big-O describes how cost grows when the input grows, ignoring the constants. It tells you which algorithm wins at scale, before anyone runs anything.
Why Does This Exist?
Two correct programs can differ in cost by a factor of a million on real inputs, and the difference is visible before either one runs. A linear scan over a billion rows does a billion steps; a quadratic comparison of the same rows does a billion billion. No machine, no optimiser, no GPU closes that gap. Complexity analysis is how you see it coming.
The tool is asymptotic notation. Big-O gives an upper bound (grows no faster than), Omega a lower bound (grows no slower than), and Theta a tight bound (both at once). When someone says attention is quadratic in sequence length, that is a Big-O claim with billion-dollar hardware consequences.
This page teaches the definitions and the counting. What those counts mean on real accelerators, where moving bytes often costs more than arithmetic, is the sequel in computational complexity for ML.
The one idea you need first
Cost means operation count as a function of input size : . We study its shape as grows large, deliberately ignoring constant factors and small- behaviour. The payoff is comparison: shapes separate while constants merely shift.
Think of It Like This
Handshake rounds at parties
At a party, everyone shaking everyone else's hand takes about handshakes for guests. Ten guests: 45 handshakes, fine. A hundred guests: 4,950, and the evening is over.
Now change the ritual: everyone shakes only the host's hand. That is handshakes. A hundred guests means a hundred handshakes, and doubling the guest list merely doubles the work.
The first ritual is quadratic, the second linear, and no faster hand-shaking closes the gap as parties grow. Big-O is the vocabulary that says so in one symbol: versus .
How It Actually Works
The three bounds, stated plainly:
- if for all large , for some constant . Upper bound: no faster growth than .
- if for all large . Lower bound: no slower growth than .
- if both hold. Tight bound: same growth shape as .
In practice the fastest-growing term decides everything: in , the term dominates past small , the constants 3, 5, 7 wash out, and the whole thing is . Formally, for all (upper, ) and always (lower, ).
Worked example
Double the input from to and watch each class respond:
- constant : 5 operations stay 5. Ratio 1.
- linear : 8 becomes 16. Ratio 2.
- quadratic : 64 becomes 256. Ratio 4.
- cubic : 512 becomes 4,096. Ratio 8.
That ratio column is the whole game. A quadratic matrix multiply at does about multiply-adds ( for matmul, strictly speaking, hence the ratio-8 row), which is why sequence length dominates transformer cost and why linear-time alternatives are such a prize.
Code
The point is the check: confirm the doubling ratios empirically with raw operation counts, no timers involved.
def op_count(n: int, kind: str) -> int: if kind == "linear": return n if kind == "quadratic": return n * n raise ValueError("unknown kind")
for kind in ("linear", "quadratic"): print(kind, op_count(16, kind) / op_count(8, kind)) # -> linear 2.0, quadratic 4.0The printed ratios 2.0 and 4.0 match the theory exactly: doubling doubles linear work and quadruples quadratic work. Constants would cancel in the division even if the code counted them.
Watch Out For
Quoting Big-O while the constants decide the race
An method with a constant of 10,000 loses to an method with a constant of 1 for every below 10,000. Asymptotics describe large ; your may not be large. Always ask what you actually run before letting the notation pick the algorithm.
Confusing worst case with the case you have
Quicksort is worst case and average case, and hash tables are until an adversary picks the keys. State which case a bound describes, and check your inputs land in it. Interview answers that skip this qualifier are where the follow-up question bites.
The Quick Version
- Big-O bounds growth from above, Omega from below, Theta from both sides at once.
- Only the fastest-growing term matters: is Theta of .
- Doubling doubles linear cost, quadruples quadratic cost and multiplies cubic cost by eight.
- Constants and small- behaviour survive outside the notation, so check your actual .
- Name the case: worst, average and amortised bounds answer different questions.