Algorithm vs constant-factor wins

P34.optimisation.01 · Audience: guest, it-ml, language-pro · Prerequisites: Find the hot spot first

Real LLM grading for this pageLLM grading (this page):

The profile told you where the time goes. Now you fix it — and the order matters. The biggest wins almost always come from algorithmic complexity, not from micro-tuning. A single O(n^2) loop loses to an O(n) one on any input large enough to care about, no matter how tight the inner code is.

Step 1 / 3Algorithm first
ⓘ Concept: Complexity dominates on large inputs
Dropping from O(n^2) to O(n log n) or O(n) is where the order-of-magnitude wins live: a better data structure (a set or dict for membership, a heap for top-k) or avoiding repeated work (caching / functools.lru_cache). Fix the complexity class before you touch anything else.

Why it matters — No amount of vectorising rescues a quadratic algorithm — get the Big-O right and the rest is polish.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

2 graded rungs · ~20 min

Rewrite a quadratic hot loop to linear against a metered CPU budget (kernel), then reason through the algorithm-vs-constant-factor trade-off.

Rung 1 — kill the quadratic (kernel, metered)

Loading exercise…

Rung 2 — algorithm vs constant-factor (interview)

Loading exercise…

The capstone brings the whole pillar together: profile a slow module, find the hot spot, and optimise it under a target budget — the way you would in a real performance ticket.