Big-O notation describes how an algorithm's time or space scales with input size , ignoring constants and lower-order terms.
Common classes (fastest to slowest):
| Class | Name | Example in ML |
|---|---|---|
| Constant | Hash table lookup, embedding index | |
| Logarithmic | Binary search for threshold tuning | |
| Linear | Single pass over dataset, forward pass of one layer | |
| Linearithmic | Sorting logits for top-, FFT in signal processing | |
| Quadratic | Self-attention over sequence length | |
| Cubic | Naive matrix multiply, SVD of matrix | |
| Exponential | Brute-force search over all subsets |
Space complexity matters too: storing all attention scores takes memory. FlashAttention reduces this to by recomputation.
Amortized analysis: some operations are expensive occasionally but cheap on average. Example: appending to a dynamic array is amortized despite occasional resizes. KV-cache growth in autoregressive generation follows a similar pattern.
What to internalize:
- A dense layer on costs FLOPs
- Self-attention is where = sequence length, = head dimension
- Knowing complexity tells you what will break at scale — if it's , doubling quadruples cost
- Constants matter in practice: with small constant can beat with large constant for realistic
See also: Computational Complexity of Attention, P vs NP and Intractability