Module 2 · Measuring Speed and Memory · Lesson 3 of 40

Big-O: how work grows

Count steps instead of seconds, and describe how the steps grow as the input gets bigger.

In this lesson · about 20 minutes

  • Explain what n means
  • Read O(1), O(n) and O(n²) in plain words
  • Count the steps of a simple loop

How fast is a program? Seconds are a bad measure: a new laptop is faster than an old phone. Instead we ask a better question:

"When the input gets bigger, how fast does the number of steps grow?"

This is called time complexity.

What is n?

We use the letter n for the size of the input. For a list, n is the number of items. For a word, n is the number of letters. If there are two different inputs, we use two letters, like n and m.

Big-O

We write the growth with Big-O. Read O(n) as "order n". It describes the worst case, the most work the code could need. Here are the most common ones, from fastest to slowest:

Big-ONamePlain wordsExample
O(1)constantSame work, any sizeRead nums[5]
O(log n)logarithmicCut the problem in half each stepBinary search
O(n)linearLook at each item onceFind the biggest number
O(n log n)n log nA bit more than linearGood sorting (merge sort)
O(n²)quadraticCompare every item with every itemTwo loops, one inside the other
O(2ⁿ)exponentialDoubles with every extra itemTrying every subset

Try it · see it work

How fast does the work grow?

Pick a size for n. Watch how quickly O(n²) and O(2ⁿ) get out of hand.

Input size n =
O(1)1 step · instant
O(log n)10 steps · instant
O(n)1,000 steps · 1.0 microseconds
O(n log n)10,000 steps · 10.0 microseconds
O(n²)1,000,000 steps · 1.0 milliseconds
O(2ⁿ)astronomical steps · longer than a million years

Times assume a computer that does about one billion simple steps a second. Bars use a squeezed (log) scale so they all fit; the real gaps are far bigger. Try n = 1,000,000 and look at O(n²).

Counting steps

Let's count. This loop prints every item once:

for x in nums:
    print(x)

If nums has n items, the loop runs n times. So it is O(n).

This code has a loop inside a loop:

for a in nums:
    for b in nums:
        print(a, b)

For each of the n values of a, the inner loop runs n times. That is n × n = n² steps, so it is O(n²).

And this line does one step no matter how long the list is:

first = nums[0]

That is O(1).

Try it: count the steps yourself

Run it, then change n to 20 and 40. Watch how the two counts grow differently.

Exercise

Count the pairs

Write count_steps(n) that returns how many times the inner line runs in this code, by actually running loops and counting:

for i in range(n):
    for j in range(n):
        ...  # this line

Check your understanding0 of 4 correct

Question 1

1.Code reads nums[0] once. What is its time complexity?

Question 2

2.A loop looks at every item in a list once. What is it?

Question 3

3.A loop inside a loop, both over the same list of n items, is…

Question 4

4.Why do we count steps instead of seconds?

Quiz score: 0 of 4

Exercises solved: 0 of 1

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. This course follows the topics of a set of DSA study notes, rewritten in Noviq's own words with Python examples. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.