Reading Complexity at a Glance

Classifying code rarely takes an exact count. Find the part that grows fastest with the input and ignore the rest. A short set of rules covers most of what you will read in pipeline code.

Rules for reading growth

Why constants and small terms drop out

Big O describes the shape of growth at large n, where only the biggest term matters. Take code that does n² + 3n + 5 steps. At n = 1,000,000 the n² term is 1,000,000,000,000, while 3n + 5 is about 3,000,000, less than a thousandth of a percent of the total. Calling it O(n²) loses nothing you would act on. Likewise 3n and n / 2 both double when n doubles, so both are O(n).

steps countedBig Owhy
5O(1)no n at all
3n + 5O(n)drop the factor 3 and the + 5
n² + 3n + 5O(n²)n² outgrows everything else
n × mO(n × m)2 independent inputs keep both letters
n + mO(n + m)either input can be the larger one

Classifying real snippets

Classify each function yourself before reading the explanation under it.

def first_and_last(events):
    first = events[0]
    last = events[len(events) - 1]
    return [first, last]

O(1). len reads a stored size and indexing jumps to a slot, so the function costs the same for 10 events as for 10 million.

def first_week(daily_counts):
    week = []
    for day in range(7):
        week.append(daily_counts[day])
    return week

O(1). It has a loop, but the loop runs exactly 7 times however long daily_counts is. A fixed number of iterations is constant work.

def users_also_in(today, yesterday):
    both = []
    lookup = set(yesterday)
    for user in today:
        if user in lookup:
            both.append(user)
    return both

O(n + m). Building lookup walks yesterday once (m steps), then the loop does constant work per user in today (n steps). Check against yesterday directly in the if and it becomes O(n × m).

Do
  • Convert lookup lists to a set or dict before the loop
  • Compute totals and maxes once, outside the loop that uses them
  • Ask what happens to the step count when the input doubles
  • Test with 10 times the sample size before shipping
Avoid
  • Use in on a list inside a loop over rows, or call the list's count or remove there
  • Assume a one-line call costs a single step
  • Read a loop over range(7) as O(n)
  • Trust a 1,000-row test to predict a 50-million-row run

plays has n items and artists has m. What's the growth rate of top_artist?

def top_artist(plays, artists):
    best = artists[0]
    for artist in artists:
        if plays.count(artist) > plays.count(best):
            best = artist
    return best

Nesting with a fixed inner loop

Nested loops multiply their counts, and the product is n² only when both counts grow with the input. In this check, the inner loop walks 4 field names that are the same for every row:

def blank_fields(rows):
    blanks = 0
    for row in rows:
        for field in ["artist", "track", "country", "device"]:
            if row[field] == "":
                blanks = blanks + 1
    return blanks

n rows times 4 fields is 4n steps, so blank_fields is O(n). Replace the field list with a loop over rows and both counts become n, giving n × n: O(n²). Before multiplying, ask of every inner loop what it walks over.

This function has a loop inside a loop. How does its work grow with the number of rows?

def retry_flags(rows):
    flags = []
    for row in rows:
        for attempt in range(3):
            flags.append(row)
    return flags

About this lesson

This section is part of the Complexity: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.