Performance Scaling
How algorithms grow with input size
Before formal definitions, let's build intuition. Suppose you have an algorithm
that takes seconds on input of size . You scale the input by a factor
of 10 — what happens?
| Scaling | Constant | Linear | Quadratic | Exponential |
|---|---|---|---|---|
| 1 | 10 | 100 | 1024 | |
| 1 | 100 | 10 000 | ||
| 1 | 1000 | astronomically huge |
The lesson. A linear-time algorithm on is 10× slower than on
. A quadratic one is 100× slower. An exponential one becomes
impossible.
Practical examples:
- Looking up a name in a sorted list (binary search): logarithmic.
- Sorting items: — near-linear.
- Checking all pairs in a list: quadratic.
- Solving a traveling salesman by brute force: factorial.
The "one year" example. Suppose algorithm A does operations,
algorithm B does operations. On a machine doing ops/sec, B is
100× slower than A. If A takes 5 minutes, B takes 500 minutes — about 8
hours. If C does operations, on it does
ops — fine. On it does — about 8 days.
The growth class matters far more than constants. We capture this formally
with Landau sets next.
- 0.0sGrowth of Common Functions
- 0.8sAxes drawn: n on the horizontal, f(n) on the vertical.
- 1.8sConstant 1: flat baseline.
- 2.6slog n: barely rises across the entire range.
- 3.4sn: a steady diagonal line.
- 4.2sn squared: curves up gently at first, steepens later.
- 5.0s2 to the n: explodes exponentially and dwarfs every other curve.
- 6.4sExponential eventually overtakes everything.