Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Time complexity and counting steps

After this lesson: I can count the steps of an algorithm and say how the count grows.

55 min lesson4 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

How many steps does a search take?

An algorithm is a list of steps that solves a problem. Time complexity says how the number of steps grows as the input gets bigger. It matters because a method that feels instant on ten items can freeze on ten million.

A shop's search page looks through its products every time someone types. Counting the steps tells you, before you run anything, whether it will still work when the shop grows.

By the end of this lesson

Note

I can count the steps of an algorithm and say how the count grows.

Key words

WordWhat it meansExample
Algorithma list of steps that solves a problem
Stepone small action, such as one comparison
Input sizehow much data the algorithm is given, called nn = 1000
Time complexityhow the number of steps grows as n grows
Big O notationa short way to write the growthO(n)
Growthhow much the steps rise when n rises

The cells students fill in are left blank in this preview.

We count steps, not seconds

A step is one small action: one comparison, one addition, one list item looked at. Seconds change with the machine, but the number of steps does not.

So to judge an algorithm you count its steps for an input of size n. For a list, n is the number of items.

Count the steps of a loop

Code: python
# Count the steps of a loop that adds up some prices.
prices = [4, 9, 2, 7, 5]
steps = 0
total = 0
for price in prices:
    total = total + price
    steps = steps + 1
print("total", total, "steps", steps)
# Change prices to ten numbers, then press Run again.

Growth shows how a method scales

What matters is how the count changes when n doubles. A loop over the list takes twice as many steps for twice the items. That is linear growth.

A loop inside a loop takes n times n steps. Double the items and the steps become four times as many. That is quadratic growth.

Count a loop inside a loop

Code: python
# Count the steps of a loop inside a loop.
n = 5
steps = 0
for i in range(n):
    for j in range(n):
        steps = steps + 1
print("n", n, "steps", steps)
# Change n to 10, then press Run again.

Worked example: steps as n grows

Growthn = 10n = 100n = 1000
Constant: one step111
Logarithmic: halve until one is left369
Linear: one pass101001,000
Quadratic: a loop inside a loop10010,0001,000,000

The cells students fill in are left blank in this preview.

Let the terminal count for you

In the lab, seq 1 5 writes the numbers 1 to 5, one per line, and wc -l counts lines. Together they count to five.

The second Run does the quadratic sum: a thousand rows of a thousand columns is a million cells.

Count five lines

Command
seq 1 5 | wc -l

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A thousand times a thousand

Command
python3 -c "print(1000 * 1000)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Measure the time on your own machine

Steps are exact. Time is not, because it depends on the machine and on what else it is doing.

The time word before a command prints three lines after it ends: real, user and sys. Read the real line. The numbers will be yours alone, so run it twice and expect small differences.

Time one hundred thousand steps

Command
time seq 1 100000 | wc -l

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Time ten times as many steps

Command
time seq 1 1000000 | wc -l

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Big O keeps the fastest growing part

Big O notation is a short way to write the growth. You drop the small parts and the constant numbers, because for a big n they hardly matter.

An algorithm of 3n + 5 steps is O(n), said "order n". One of n times n plus 3n steps is O(n squared).

A common mistake

Warning

Many people think the method that ran faster on their laptop is the better algorithm. Seconds depend on the machine and on the one size you tried. Compare the number of steps as n grows, because that stays true on any machine.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.