Skip to content

Analyzing loops

Find the Θ running time of a loop from its code, by counting how many times its busiest line runs, for single, nested, dependent and halving loops.

It opens on

The line count = count + 1 sits inside two loops, each over range(n). For n = 10, will count end near 20, near 100, or near 1000?

Why it matters

Almost every running time you will meet in this course is a loop, or a loop inside a loop, so reading the Θ straight off the code, before running anything, is the skill that tells you which of two programs will finish on a million items.

Good to know first

  • Counting steps: the work of a function is counted as a formula in n
  • Big-O, Big-Omega and Big-Theta: Θ keeps the fastest term and drops constants
  • Nested loops: the inner loop runs completely for each pass of the outer loop
  • while loops: the block runs while its condition is True, and a counter shows how many times

Step by step

  1. Nested and one after another

    Count the runs of the busiest line for a single loop, two nested loops and two loops one after another, and give the Θ.

    Take n = 3. For i = 0 the inner loop runs j = 0, 1 and 2, so the counter line runs 3 times; i = 1 and i = 2 do the same, so the line runs 3 + 3 + 3 = 9 times.

    A common mix-up

    Two loops always make n².

    In fact: Only loops inside each other multiply; two loops one after another add to 2n, which is Θ(n).

  2. The inner loop depends on i

    Count the runs of the busiest line when the inner loop runs i times, derive n(n − 1)/2 and show that it is Θ(n²).

    Take n = 5. For i = 0 the range is empty, so the counter line runs 0 times; for i = 1 it runs once, and for i = 2 twice, which makes a total of 3 so far.

    the first three rows of the table

    A common mix-up

    Since the inner loop is shorter, the running time is smaller than n².

    In fact: It is n(n − 1)/2, half as many runs but still Θ(n²), because the constant 1/2 is dropped.

  3. The loop that halves

    Count how many times a loop that halves its variable runs, ⌊log₂ n⌋ + 1, and give the Θ of loops that contain it.

    Take n = 20. The first test has m = 20, which is at least 1, so the counter becomes 1 and m becomes 10; the second pass has m = 10 and then m becomes 5, with count 2; the third has m = 5.

    the first three rows of the table

    A common mix-up

    A loop that moves one step at a time and one that halves differ only by a constant.

    In fact: Halving has Θ(log n) passes against n, and for n = 1,000,000 that is 20 against a million.

Try it

A pair of loops over range(n) nested in each other is followed by one more loop over range(n). How many runs of the counter for n = 6, and what Θ?Answer

42, which is 36 + 6, and Θ(n²)

Hint: Multiply the nested pair, then add

For the loops with range(i) and n = 6, how many times does the counter line run?Answer

15

Hint: 6 × 5 / 2

How many passes does the halving loop make for n = 37?Answer

6

Hint: 37, 18, 9, 4, 2, 1

How many times does the counter line run for n = 5, and for n = 10?Answer

25 and 100, so the count is n² and the program is Θ(n²).

For i = 3 how many inner runs, and what is the total so far?Answer

3 inner runs, and the total is 6.

Analyzing loops

30 minutes every month. No card.