Theory answers "how does time grow as n grows?", while measurement shows the actual time on an actual machine. A good engineer combines both: derive an expectation from analysis and check it by measuring. In this lesson we build small tools for checking complexity empirically.

The measuring protocol

  1. Pick sizes of n and keep doubling them: n, 2n, 4n, ....
  2. Prepare the input data outside the timer.
  3. Repeat several times for each size and take the minimum: it is the least noisy.
  4. Compute the ratio T(2n) / T(n) or the slope on a log-log scale.
  5. Compare the result with the theoretical expectation.
ComplexityExpected T(2n) / T(n)log-log slope
O(1)≈ 10
O(log n)≈ 1 (slightly more)≈ 0
O(n)21
O(n log n)a bit above 2a bit above 1
O(n^2)42
O(n^3)83
O(2^n)T(n + 1) / T(n) = 2not a straight line

A benchmark function and ratios

bench returns the best of the repeats. ratio shows how many times the time grows when the size doubles. Results depend on the machine, so we check the answer against a range, not exact seconds.

python
import random
from time import perf_counter