sciandu
Computer science

Computer science

How fast is a program?

Counting steps instead of measuring seconds: why some programs handle big data with ease while others take forever.

What you need first

Why does one app load instantly while another grinds forever? Often it's not your phone, but how cleverly the program computes. You already counted steps while searching and sorting. Now we turn that into a real superpower: you'll learn to predict how a program behaves as the data grows.

Steps instead of seconds

Measuring seconds is unfair: on a fast computer the same program runs quicker than on an old phone. That's why computer scientists prefer to count steps, for example comparisons. The number of steps is the same on every device. That way you can compare two methods without even running them.

How does the effort grow with n?

The exciting question is not how many steps a program needs for 10 entries, but what happens when 10 entries become 10000. We call the input size n. With the step count grows directly with n: twice as many entries, twice as many steps. With , each doubling of n adds only a single step, because every step halves the list. One condition matters: halving only works if the list is sorted.

Search race
n100,000

Linear search: 100,000 steps

Binary search: 17 steps

Bars on a logarithmic scale

At n = 100,000: 100,000 vs 17 steps
Try it: increase n and watch how differently fast the two curves grow.

Linear versus halving

For a list of 8 entries the difference is tiny: 8 steps versus 3 steps, who cares? But let n grow. At 1024 entries it's 1024 versus 10. At a million entries roughly a million versus 20. The halving method grows so slowly that even huge inputs barely matter. This growth behaviour is called logarithmic.

Why this decides everything

Search engines, map services, and streaming providers work with billions of entries. At such sizes, growth behaviour decides whether an answer arrives in milliseconds or only after hours. A faster computer helps a little, a better method helps enormously. That's why counting steps is one of the most important ideas in computer science.

Exercises

0 of 6 solved

Time to try it yourself. You can't break anything, every attempt counts.

Why do we count steps instead of measuring seconds?

A program needs one step per entry. How many steps does it need for n = 50?

Linear search needs at most 100 steps for 100 entries. How many steps does it need at most for 200 entries?

A method that halves the list at every step grows so slowly that each doubling of n costs only one more step. This growth behaviour is called .

Which method grows slowest as n gets bigger?

Order the three methods by how their effort grows as n gets bigger: from slowest growth to fastest.

  1. 1Halving the list at every step
  2. 2Comparing every entry with every other one
  3. 3Looking at every entry once

Where this leads