Big-O: See at a Glance How Long Code Will Run
The sister part covered “data structure = how you store things.” This chapter covers the other half: algorithm = a playbook for getting work done. To judge whether a playbook is any good, programmers share one ruler: Big-O. Don't let the math symbols scare you—it answers only one question: when the data grows, how much slower does your code get? Today, three interactives will install that ruler in your head.
Time curves for four common “playbooks”: O(1) gray (one step no matter how much data), O(log n) green (cut in half each time), O(n) blue (walk through one by one), O(n²) red (everyone compared with everyone). Drag the slider from 10 to 100,000; the right side converts to real time at “100 million ops/sec.” Watch: early on, all four lines pile together—when data is small every algorithm looks fast, which is exactly why demos lie.
A more direct question: your boss says “users will 10×”—how much slower does each playbook get? Hit “×10,” click three times and watch the gap snowball.
You've got the ruler—time to try it. Three snippets of pseudocode; pick a complexity for each. Trick: don't read every line—just ask “when data grows, how much more work does it do?”
What this lesson wants to share
- Big-O only tracks the trend: it doesn't care how fast one run is—only how time grows as data grows
- Constants don't matter; trends kill: 2× slower is fine; growing with n² is a death sentence
- Small data hides the gap: four curves pile together in the demo stage; the explosion waits until data grows
- n² causes most lag accidents: when reviewing code, hunt nested loops first