Lite / Intelligent Robotic Systems / Week 6
Module check: measure growth, then explain it
Your model says the planner is O(n). The stopwatch says doubling the map quadruples the time. Who's right?
Why should I care?
Big-O is a prediction; timings are evidence. Good engineers measure to check the prediction, and when they disagree, they find out why. This module check brings weeks 4–6 together in one honest experiment.
- Time code fairly: repeat runs, report the best or median, and keep everything else fixed.
- Estimate a growth rate from the ratio of times when n doubles.
- Explain what big-O does not tell you: constants, small inputs, memory and hardware effects.
Predict
You time a function at n = 1,000 (12 ms), 2,000 (47 ms) and 4,000 (190 ms). What growth does this suggest?
- O(n)
- O(n log n)
- O(n²)
- O(log n)
Guess first, then reveal
O(n²): each doubling multiplies the time by about 4 (47/12 ≈ 3.9, 190/47 ≈ 4.0). O(n) would double and O(n log n) would give a little over 2.
Discover
In the Code Lab, time your visited-as-list BFS and your visited-as-set BFS on open grids of 20 × 20, 40 × 40, 80 × 80 (and 160 × 160 if it's quick). Use the best of five runs. Each step doubles the side, so the number of cells quadruples. What time ratio does each version show per step, and what growth does that suggest?
What the interactive shows (text version)
The Code Lab experiment can be done from a table: list-based visited checks grow roughly 4 times when the number of cells doubles (quadratic), while set-based visited checks grow roughly 2 times (linear). The worked example's duplicate counter shows the same pattern, with an estimated exponent near 2 for the list and near 1 for the set.
Understand
Fair timing
Use time.perf_counter() or timeit; run each size several times and report the best or the median (the first run is often slow while Python warms up). Change only n. Close other heavy programs. Record the machine and Python version so others can compare.
Read growth from ratios
When the input doubles, the time ratio tells you the growth: about 2 for O(n), a bit over 2 for O(n log n), about 4 for O(n²). Plotting time against n on log–log axes turns a power law into a straight line whose slope is the exponent.
What big-O won't tell you
Constants (C code inside Python is far faster than Python loops), memory and caching effects, small-input overheads and noise from other programs. A careful report states the prediction, the measurement and an explanation of any gap.
Explain it another way
Intuitive
Weighing yourself once on a wobbly floor tells you little. Weigh several times, on a firm floor, and look at the trend over weeks.
Technical
If T(n) ≈ c·n^p, then log T = log c + p·log n, so p ≈ log(T₂/T₁)/log(n₂/n₁). With doubling, p ≈ log₂(T₂/T₁). Noise of a few per cent is normal; use several sizes, not two.
Mathematical
From 12, 47 and 190 ms: p ≈ log₂(47/12) ≈ 1.97 and log₂(190/47) ≈ 2.02, so p ≈ 2.
Applied
Performance regressions in large codebases are usually caught by benchmarks that run at several input sizes and alert when the growth exponent changes, not just when one time gets slower.
Worked example 1
Time a list-membership check versus a set at several sizes and estimate the exponent.
import math
import time
def best_time(fn, repeats=5):
best = float("inf")
for _ in range(repeats):
t0 = time.perf_counter()
fn()
best = min(best, time.perf_counter() - t0)
return best
def count_dupes_list(xs):
seen, dupes = [], 0
for x in xs:
if x in seen: # scans the list: O(len(seen))
dupes += 1
else:
seen.append(x)
return dupes
def count_dupes_set(xs):
seen, dupes = set(), 0
for x in xs:
if x in seen: # hash lookup: O(1) on average
dupes += 1
else:
seen.add(x)
return dupes
sizes = [500, 1000, 2000]
for name, fn in [("list", count_dupes_list), ("set", count_dupes_set)]:
times = [best_time(lambda n=n: fn(list(range(n)))) for n in sizes]
p = math.log2(times[2] / times[1])
print(name, ["%.2e" % t for t in times], "exponent ~", round(p, 1))
# Typical output (exact times vary by machine): list exponent ~2, set exponent ~1Common mix-ups
One timing run is enough.
✓ Timings are noisy. Repeat, take the best or median, and use several sizes.If timings disagree with big-O, the timings are wrong.
✓ Usually the model is incomplete: look for hidden loops or costs you didn't count.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Module check deliverable. A one-page investigation: your BFS with visited as a list and as a set, timed on at least four grid sizes (best of five). Include a table, an estimated exponent for each version, your big-O prediction for each, and a paragraph explaining any disagreement. State your machine and Python version.
Reflect
What did measuring teach you that counting alone did not?
Write your answer on paper or in your own notes. Lite never stores what you write.
Go further
- Python docs: time.perf_counter
The high-resolution clock recommended for timing short pieces of code.