Sub topic 1
Measuring cost by input size, not by seconds
Two programs can solve the same problem, and one can be far cheaper than the other. This chapter sets up the way we compare them. Instead of timing a run, we count the work an algorithm does as a function of the size of its input. The rest of the tutorial builds on that idea.
Prerequisites
This chapter assumes you can:
- Read and write Python functions, loops, and simple recursion.
- Use lists (arrays) and index into them.
- Follow basic algebra, including exponents like n² and 2ⁿ.
Logarithms appear in later chapters. You don't need them yet.
Why stopwatch timings mislead
You could compare two functions by timing them. The result depends on things that have nothing to do with the algorithm:
- The processor, memory, and other programs running at the same time.
- The language, the interpreter version, and compiler settings.
- The particular input you happened to test.
A timing on your laptop tells you little about how the code behaves on a server, or on an input ten times larger. We need a measure that survives a change of machine.
Input size and counting operations
The input size, written n, is the quantity that controls how much work the algorithm has to do. For a list, n is usually its length. For a number, it is often the number's magnitude or its digit count. State which one you mean.
The cost of an algorithm is the number of basic operations it performs, written as a function T(n). A basic operation is a step that takes roughly constant time: a comparison, an arithmetic operation, or an index lookup.
The next program counts steps instead of measuring time:
def total(items):
steps = 0
s = 0
for x in items:
s += x
steps += 1 # one addition per element
return s, steps
def count_comparisons(items):
comparisons = 0
n = len(items)
for i in range(n):
for j in range(i + 1, n):
comparisons += 1
if items[i] == items[j]:
return comparisons
return comparisons
for n in (10, 100, 1000):
data = list(range(n)) # all values distinct: the worst case
_, steps = total(data)
print(n, steps, count_comparisons(data))Running it prints:
10 10 45
100 100 4950
1000 1000 499500These counts are the same on every machine, every time.
What the counts show
| n | `total` steps | `count_comparisons` steps |
|---|---|---|
| 10 | 10 | 45 |
| 100 | 100 | 4,950 |
| 1,000 | 1,000 | 499,500 |
When n grows by 10×, total does 10× the work. The pairwise function does about 100× the work, because it makes n(n−1)/2 comparisons. The counts differ in how they grow, and that difference decides which algorithm stays usable on large inputs.
A function that returns items[0] takes one step whatever n is. Its cost does not grow at all.
Common errors
- Forgetting to name n. "This is fast" means nothing until you say what n is. For a list of strings, n could be the number of strings or their total length.
- Counting everything. You don't need to count every assignment. Pick one operation that tracks the total work, such as the comparison in the inner loop, and count that.
- Judging from one input.
count_comparisonsstops early if it finds a duplicate. The numbers above use distinct values, which gives the largest count. Cost can depend on the input's contents as well as its size. A later chapter covers best, worst, and average cases. - Reading small counts as proof. Counts at n = 10 can hide the trend. Look at how the count changes as n grows.
Exercise
A function loops over a list of n items and, for each item, calls items[0] once and compares it to the item. How many comparisons does it make for n = 50 and for n = 500? By what factor does the work grow?
Show answer
It makes 50 comparisons for n = 50 and 500 for n = 500. The work grows by a factor of 10 when n grows by a factor of 10, so the cost is proportional to n. The items[0] lookup is one step per iteration and does not change the growth.