{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"big-o-in-plain-terms","title":"Big-O, and the four growth rates you will meet","url":"https://addaly.com/learn/maths-you-need/big-o-in-plain-terms","position":58,"of":76,"minutes":8,"answer":"Big-O keeps only how work grows as input doubles, and the line that matters runs between n log n, which survives any dataset, and n².","keyIdea":"Big-O keeps only how work grows as input doubles, and the line that matters runs between n log n, which survives any dataset, and n², which stops working around a million items however fast the hardware."},"body":{"format":"markdown","text":"## The question Big-O answers\n\nWhen the input doubles, what happens to the work? That is the only question Big-O notation answers. It ignores the constant factors, the hardware and the language, and keeps just the shape of the growth. That sounds like throwing away everything useful, and yet it is the single most predictive thing you can know about a piece of code before running it, because the shape decides whether a job that took a minute on a thousand rows takes an hour or a decade on a million.\n\n## Four shapes\n\nTake `n` items and a machine doing a billion simple operations a second.\n\n- **`O(log n)`**: each step halves the problem. Binary search in a sorted list; finding a key in a balanced tree. At `n = 10^9`, thirty steps. Effectively free at any scale.\n- **`O(n)`**: look at everything once. Summing a column, scanning a file, one pass of a tokeniser. At `n = 10^9`, one second.\n- **`O(n log n)`**: look at everything, a logarithmic number of times. Sorting. At `n = 10^9`, thirty seconds. This is the cost of \"sort it first\", and it is nearly always affordable.\n- **`O(n²)`**: compare everything with everything. At `n = 10^6`, `10^12` operations: seventeen minutes. At `n = 10^9`, `10^18`: thirty-one years.\n\nThe gap between the third and fourth lines is the gap that matters. Anything up to `n log n` scales to any dataset you will meet. Anything quadratic scales to about a million and then stops, and the next lesson but one is about recognising it.\n\nThere is a fifth shape, `O(2^n)`, for problems where every subset must be tried. At `n = 60` it is thirty years; at `n = 100` it is longer than the universe. Exact solutions to such problems are not slow; they are unavailable, and the field's answer is to approximate.\n\n## Why the constants are dropped, and when they matter\n\n`3n² + 200n + 5000` is `O(n²)` because for large `n` the first term is all that counts: at `n = 10^6`, the `3n²` is `3 × 10^12` and the rest is `2 × 10^8`, a rounding error. Big-O keeps the term that wins.\n\nBut the constants are real, and two `O(n)` implementations can differ by a factor of a hundred. A Python loop adding a million numbers takes about 50 milliseconds; `numpy.sum` on the same array takes half a millisecond. Same shape, hundred-fold constant. The rule: **Big-O tells you whether the approach can work; the constant tells you whether it will be pleasant.** Fix the shape first, because no constant rescues `n²` at a billion. Then fix the constant, because a hundredfold is a hundredfold.\n\n## The trap in one character\n\n```python\nseen = []                     # list\nfor item in stream:\n    if item in seen:          # O(n) scan every time\n        continue\n    seen.append(item)\n```\n\n`item in seen` on a list scans the whole list. Inside a loop over `n` items, that is `n` scans of up to `n` elements: `O(n²)`. Change one word:\n\n```python\nseen = set()                  # set\n```\n\nand `item in seen` becomes a hash lookup, `O(1)`, and the loop is `O(n)`. On a million items the first version takes hours, the second a second. This is the most common accidental quadratic in data code, and the lesson on hashing explains what the set is doing that the list is not.\n\n## Memory has a shape too\n\nThe same notation describes space. A similarity matrix between `n` embeddings has `n²` entries. At `n = 10^5` and four bytes each, that is 40 GB, which does not fit in a laptop's RAM, and the code that builds it will not fail with a helpful message; it will swap for an hour and then be killed. A pairwise matrix is `O(n²)` memory whatever you do with it, and the fix is the same as for time: do not build it.\n\n## Amortised, and average\n\nTwo refinements you will meet. Appending to a Python list is `O(1)` **amortised**: occasionally the list must be copied to a bigger block, which costs `O(n)`, but that happens rarely enough that the average per append is constant. And hash lookups are `O(1)` **on average**; a pathological set of keys that all collide gives `O(n)`, which is why languages randomise their hash functions.\n\n## What Big-O cannot see\n\nIt cannot see the difference between reading from RAM and reading from disk, which is a factor of a thousand. It cannot see that a GPU does a thousand things at once, so an `O(n)` loop and an `O(n)` matrix operation take very different times. It cannot see cache. The lesson on arithmetic intensity is about exactly the costs Big-O is blind to. Use both: Big-O for whether the shape survives scale, and the arithmetic of bytes and FLOPs for how long it will actually take.\n\n## The habit\n\nBefore running anything on the full data, ask: when `n` doubles, does this double, or quadruple? Time it on a thousand rows and on two thousand. If the second run takes four times as long, you have a quadratic, and now is the moment to fix it, not at a million."},"check":{"question":"A deduplication script runs in 4 seconds on 10,000 records and 16 seconds on 20,000. What should you expect on a million records, and why?","options":[{"text":"About 400 seconds, since a million is a hundred times 10,000.","correct":false,"tempting":"Assumes linear growth; doubling the input quadrupled the time, which is the signature of n², not n."},{"text":"About 40,000 seconds: time quadruples when input doubles.","correct":true},{"text":"It cannot be predicted, because Big-O ignores constants and hardware.","correct":false,"tempting":"Big-O drops constants but the two timings supply them; the shape plus one measured point predicts the scale to within a small factor."},{"text":"About 800 seconds, allowing for some overhead on top of linear growth.","correct":false,"tempting":"Treats a fourfold jump as overhead rather than as evidence of the growth rate; the ratio between the two runs is the whole signal."}]},"prev":{"slug":"regression-to-the-mean","title":"Regression to the mean, and the winner's curse in model selection","url":"https://addaly.com/learn/maths-you-need/regression-to-the-mean"},"next":{"slug":"flops-of-a-matrix-multiply","title":"Counting the floating-point operations in a layer and a training run","url":"https://addaly.com/learn/maths-you-need/flops-of-a-matrix-multiply"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Big-O, and the four growth rates you will meet', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/big-o-in-plain-terms (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Big-O, and the four growth rates you will meet","courseTitle":"The Maths You Actually Need","module":{"index":7,"slug":"cost-and-scale","title":"Counting the cost: complexity and the arithmetic of scale"},"answer":null,"keyIdea":"Big-O keeps only how work grows as input doubles, and the line that matters runs between n log n, which survives any dataset, and n², which stops working around a million items however fast the hardware.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"the-question-big-o-answers","runs":[{"text":"The question Big-O answers"}]},{"t":"p","runs":[{"text":"When the input doubles, what happens to the work? That is the only question Big-O notation answers. It ignores the constant factors, the hardware and the language, and keeps just the shape of the growth. That sounds like throwing away everything useful, and yet it is the single most predictive thing you can know about a piece of code before running it, because the shape decides whether a job that took a minute on a thousand rows takes an hour or a decade on a million."}]},{"t":"h2","id":"four-shapes","runs":[{"text":"Four shapes"}]},{"t":"p","runs":[{"text":"Take "},{"text":"n","code":true},{"text":" items and a machine doing a billion simple operations a second."}]},{"t":"ul","items":[[{"text":"O(log n)","b":true,"code":true},{"text":": each step halves the problem. Binary search in a sorted list; finding a key in a balanced tree. At "},{"text":"n = 10^9","code":true},{"text":", thirty steps. Effectively free at any scale."}],[{"text":"O(n)","b":true,"code":true},{"text":": look at everything once. Summing a column, scanning a file, one pass of a tokeniser. At "},{"text":"n = 10^9","code":true},{"text":", one second."}],[{"text":"O(n log n)","b":true,"code":true},{"text":": look at everything, a logarithmic number of times. Sorting. At "},{"text":"n = 10^9","code":true},{"text":", thirty seconds. This is the cost of “sort it first”, and it is nearly always affordable."}],[{"text":"O(n²)","b":true,"code":true},{"text":": compare everything with everything. At "},{"text":"n = 10^6","code":true},{"text":", "},{"text":"10^12","code":true},{"text":" operations: seventeen minutes. At "},{"text":"n = 10^9","code":true},{"text":", "},{"text":"10^18","code":true},{"text":": thirty-one years."}]]},{"t":"p","runs":[{"text":"The gap between the third and fourth lines is the gap that matters. Anything up to "},{"text":"n log n","code":true},{"text":" scales to any dataset you will meet. Anything quadratic scales to about a million and then stops, and the next lesson but one is about recognising it."}]},{"t":"p","runs":[{"text":"There is a fifth shape, "},{"text":"O(2^n)","code":true},{"text":", for problems where every subset must be tried. At "},{"text":"n = 60","code":true},{"text":" it is thirty years; at "},{"text":"n = 100","code":true},{"text":" it is longer than the universe. Exact solutions to such problems are not slow; they are unavailable, and the field’s answer is to approximate."}]},{"t":"h2","id":"why-the-constants-are-dropped-and-when-they-matter","runs":[{"text":"Why the constants are dropped, and when they matter"}]},{"t":"p","runs":[{"text":"3n² + 200n + 5000","code":true},{"text":" is "},{"text":"O(n²)","code":true},{"text":" because for large "},{"text":"n","code":true},{"text":" the first term is all that counts: at "},{"text":"n = 10^6","code":true},{"text":", the "},{"text":"3n²","code":true},{"text":" is "},{"text":"3 × 10^12","code":true},{"text":" and the rest is "},{"text":"2 × 10^8","code":true},{"text":", a rounding error. Big-O keeps the term that wins."}]},{"t":"p","runs":[{"text":"But the constants are real, and two "},{"text":"O(n)","code":true},{"text":" implementations can differ by a factor of a hundred. A Python loop adding a million numbers takes about 50 milliseconds; "},{"text":"numpy.sum","code":true},{"text":" on the same array takes half a millisecond. Same shape, hundred-fold constant. The rule: "},{"text":"Big-O tells you whether the approach can work; the constant tells you whether it will be pleasant.","b":true},{"text":" Fix the shape first, because no constant rescues "},{"text":"n²","code":true},{"text":" at a billion. Then fix the constant, because a hundredfold is a hundredfold."}]},{"t":"h2","id":"the-trap-in-one-character","runs":[{"text":"The trap in one character"}]},{"t":"code","lang":"python","label":"Python","text":"seen = []                     # list\nfor item in stream:\n    if item in seen:          # O(n) scan every time\n        continue\n    seen.append(item)"},{"t":"p","runs":[{"text":"item in seen","code":true},{"text":" on a list scans the whole list. Inside a loop over "},{"text":"n","code":true},{"text":" items, that is "},{"text":"n","code":true},{"text":" scans of up to "},{"text":"n","code":true},{"text":" elements: "},{"text":"O(n²)","code":true},{"text":". Change one word:"}]},{"t":"code","lang":"python","label":"Python","text":"seen = set()                  # set"},{"t":"p","runs":[{"text":"and "},{"text":"item in seen","code":true},{"text":" becomes a hash lookup, "},{"text":"O(1)","code":true},{"text":", and the loop is "},{"text":"O(n)","code":true},{"text":". On a million items the first version takes hours, the second a second. This is the most common accidental quadratic in data code, and the lesson on hashing explains what the set is doing that the list is not."}]},{"t":"h2","id":"memory-has-a-shape-too","runs":[{"text":"Memory has a shape too"}]},{"t":"p","runs":[{"text":"The same notation describes space. A similarity matrix between "},{"text":"n","code":true},{"text":" embeddings has "},{"text":"n²","code":true},{"text":" entries. At "},{"text":"n = 10^5","code":true},{"text":" and four bytes each, that is 40 GB, which does not fit in a laptop’s RAM, and the code that builds it will not fail with a helpful message; it will swap for an hour and then be killed. A pairwise matrix is "},{"text":"O(n²)","code":true},{"text":" memory whatever you do with it, and the fix is the same as for time: do not build it."}]},{"t":"h2","id":"amortised-and-average","runs":[{"text":"Amortised, and average"}]},{"t":"p","runs":[{"text":"Two refinements you will meet. Appending to a Python list is "},{"text":"O(1)","code":true},{"text":" "},{"text":"amortised","b":true},{"text":": occasionally the list must be copied to a bigger block, which costs "},{"text":"O(n)","code":true},{"text":", but that happens rarely enough that the average per append is constant. And hash lookups are "},{"text":"O(1)","code":true},{"text":" "},{"text":"on average","b":true},{"text":"; a pathological set of keys that all collide gives "},{"text":"O(n)","code":true},{"text":", which is why languages randomise their hash functions."}]},{"t":"h2","id":"what-big-o-cannot-see","runs":[{"text":"What Big-O cannot see"}]},{"t":"p","runs":[{"text":"It cannot see the difference between reading from RAM and reading from disk, which is a factor of a thousand. It cannot see that a GPU does a thousand things at once, so an "},{"text":"O(n)","code":true},{"text":" loop and an "},{"text":"O(n)","code":true},{"text":" matrix operation take very different times. It cannot see cache. The lesson on arithmetic intensity is about exactly the costs Big-O is blind to. Use both: Big-O for whether the shape survives scale, and the arithmetic of bytes and FLOPs for how long it will actually take."}]},{"t":"h2","id":"the-habit","runs":[{"text":"The habit"}]},{"t":"p","runs":[{"text":"Before running anything on the full data, ask: when "},{"text":"n","code":true},{"text":" doubles, does this double, or quadruple? Time it on a thousand rows and on two thousand. If the second run takes four times as long, you have a quadratic, and now is the moment to fix it, not at a million."}]}],"check":{"question":[{"text":"A deduplication script runs in 4 seconds on 10,000 records and 16 seconds on 20,000. What should you expect on a million records, and why?"}],"options":[{"text":[{"text":"About 400 seconds, since a million is a hundred times 10,000."}],"correct":false,"tempting":[{"text":"Assumes linear growth; doubling the input quadrupled the time, which is the signature of n², not n."}]},{"text":[{"text":"About 40,000 seconds: time quadruples when input doubles."}],"correct":true,"tempting":null},{"text":[{"text":"It cannot be predicted, because Big-O ignores constants and hardware."}],"correct":false,"tempting":[{"text":"Big-O drops constants but the two timings supply them; the shape plus one measured point predicts the scale to within a small factor."}]},{"text":[{"text":"About 800 seconds, allowing for some overhead on top of linear growth."}],"correct":false,"tempting":[{"text":"Treats a fourfold jump as overhead rather than as evidence of the growth rate; the ratio between the two runs is the whole signal."}]}],"order":[0,3,1,2],"why":null},"prev":{"slug":"regression-to-the-mean","title":"Regression to the mean, and the winner’s curse in model selection","minutes":9},"next":{"slug":"flops-of-a-matrix-multiply","title":"Counting the floating-point operations in a layer and a training run","minutes":9}}}