Skip to content
Tactiverge

School/✦ Research Engineer/Week 19

How fast does the work grow?

Your code sorts 1,000 readings in a millisecond. Will a million readings take two seconds, or seventeen minutes?

About 40 min · 3 goals · Checked by automated tests

1.Why should I care?

A benchmark on today's input tells you almost nothing about tomorrow's. Asymptotic analysis does: it says how the work grows, which is what decides whether a perception pipeline, a planner or a database survives contact with real data. It is the first question a reviewer asks about any algorithm.

  • ◇ Count the basic operations of a loop nest and express the count in Big-O, Big-Ω and Big-Θ notation.
  • ◇ Distinguish best, worst and average cases, and time from space complexity.
  • ◇ Predict how run time scales when the input grows or the machine gets faster.

2.Discover

Set n = 10, then 30, then 60. Watch which bars turn orange (over a second) and red (over a year). Then make the machine 1,000× faster and see how much bigger an input each algorithm can now handle in one second.

Loading the interactive…

Can't use the interactive? Read a description instead

The racer lists seven growth rates. At n = 20 on a billion-operations-per-second machine: log n and n are nanoseconds, n² is 400 ns, n³ is 8 µs, 2ⁿ is about 1 ms, n! is about 77 years. A 1,000× faster machine lets n² handle 32× bigger inputs but 2ⁿ only about 10 more items.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

How many times is work() called when n = 16? Code: i = n; while i > 1: { work(); i = floor(i / 2) }

calls

4.Predict

Algorithm A does 100·n operations; algorithm B does n². For which inputs is A faster?

Guess first. Being wrong here is how the surprise works.

5.Understand

Count operations, not seconds

Pick a basic operation (a comparison, a multiply, a memory read) and count how many times it runs as a function of input size n. Seconds depend on the machine; the count depends only on the algorithm. Two nested loops over n items give n² executions of the inner line; a loop that halves i each pass gives ⌊log₂ n⌋.

O, Ω and Θ

f(n) = O(g(n)) means f grows no faster than g: there are constants c and n₀ with f(n) ≤ c·g(n) for all n ≥ n₀. Ω is the matching lower bound, and Θ means both. So 3n² + 50n + 7 is Θ(n²): the leading term dominates and constants are dropped. Big-O is an upper bound, so n is also O(n²), but saying Θ(n) is more informative.

Best, worst, average

Linear search finds the item first time in the best case (Θ(1)), last in the worst (Θ(n)), and about halfway on average if every position is equally likely. Quicksort is Θ(n log n) on average but Θ(n²) in the worst case. Engineers designing real-time systems usually care about the worst case, because a deadline missed once can be a failure.

Space counts too

Memory is a resource with its own complexity. Merge sort needs Θ(n) extra space; heapsort needs Θ(1). On a microcontroller with 64 KB of RAM, an algorithm with the better time complexity can be useless if its space complexity does not fit.

Explain it another way

Imagine handing out exam papers. Giving one to each student is linear work. Having every student shake hands with every other is quadratic: double the class and the handshakes quadruple.

Worked example 1

Count the inner executions: for i in 0…n−1: for j in i+1…n−1: work() with n = 6.

For i = 0 the inner loop runs 5 times, then 4, 3, 2, 1, 0. Total 15 = 6×5/2. In general n(n−1)/2, which is Θ(n²).

Worked example 2

An n² algorithm handles n = 1,000 in 1 s. Roughly what n fits in 1 s on a machine 100× faster?

Time ∝ n², so n can grow by √100 = 10: about n = 10,000. For an n log n algorithm the gain would be a little under 100×; for 2ⁿ, only about log₂ 100 ≈ 7 more items.

Common mix-ups

  • O(n) always beats O(n²).
    ✓ Only for large enough n. Constants and lower-order terms decide small inputs, so measure there too.
  • Big-O means the exact running time.
    ✓ It is an upper bound up to a constant factor. Θ gives a tight bound; neither gives seconds.

6.Watch

→ Notice how the video compares selection sort's n² with merge sort's n log n by counting comparisons, not by timing a particular computer.

Shows how a step-by-step recipe of decisions and repeats lets a machine solve a problem such as sorting.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2

Which is the tightest description of f(n) = 4n³ + 200n² log n + 10⁶?

Practice 3

A braking controller must finish its computation within 2 ms every cycle. Which complexity matters most?

Practice 4 · a fresh version every time

How many times is work() called when n = 270? Code: for i in 0 … n−1: for j in i+1 … n−1: work()

calls

Practice 5

An n³ algorithm handles n = 200 in one second. The machine is replaced with one 8 times faster. What n now fits in one second?

Practice 6

Merge sort (Θ(n log n) time, Θ(n) extra space) and heapsort (Θ(n log n) time, Θ(1) extra space) must sort 40 KB of samples on a chip with 48 KB of free RAM. Which is safer?

8.Apply it

Pick three pieces of code you wrote this year (a filter, a search, a loop over sensor data). For each, state the basic operation, its count as a function of n, its Θ class and its extra space. Then estimate the largest n each could process in 10 ms on a 100 MHz microcontroller doing one operation per cycle.

9.Reflect

Concepts: Algorithms, searching and complexity · Lists, dictionaries and tabular data