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, while a quadratic comparison of the same rows does a billion billion. No machine, no optimiser, and no GPU closes that gap. Complexity analysis is how you see it coming.
The tool is asymptotic notation. Big-O gives an upper bound that grows no faster than, Omega gives a lower bound that grows no slower than, and Theta gives a tight bound that does both at once. When someone says attention is quadratic in sequence length, that is a Big-O claim with hardware consequences at billion-token scale.
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 , written . We study its shape as grows large, deliberately ignoring constant factors and small- behaviour. The payoff is comparison: shapes separate while constants merely shift. If summation notation is rusty, the refresher is summation and product notation.
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 means 45 handshakes, which is fine. A hundred guests means 4,950, and the evening is over.
Now change the ritual so 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 and the second is 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, where is the true cost, is the reference shape, is input size, and and are positive constants:
- if for all large . 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 , , and wash out, and the whole thing is . Formally, for all with , and it is always with .
Worked example
Double the input from to and watch each class respond:
- constant : operations stay . Ratio .
- linear : becomes . Ratio .
- quadratic : becomes . Ratio .
- cubic : becomes . Ratio .
That ratio column is the whole game. A dense matrix multiply at does about multiply-adds, since , which is why sequence length dominates transformer cost and why linear-time alternatives are such a prize. The diagram shows the first three rows: staying , doubling to , and quadrupling to .
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 and 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 loses to an method with a constant of for every below . Asymptotics describe large , and your may not be large.
Symptom is a theoretically faster method losing in production on current sizes. 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. Name the case: worst, average, and amortised bounds answer different questions.
The Quick Version
- Big-O bounds growth from above, Omega from below, and Theta from both sides at once.
- Only the fastest-growing term matters: is Theta of .
- Doubling from to moves linear to and quadratic to .
- Constants and small- behaviour survive outside the notation, so check your actual .
- Name the case: worst, average, and amortised bounds answer different questions.