Capstone: profile, then optimise to a budget

P34.optimisation.02 · Audience: guest, it-ml, language-pro · Prerequisites: Algorithm vs constant-factor wins, Memory profiling

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

This is the P34 capstone — the whole pillar in one exercise. You are handed a slow module and must bring it under a time budget without changing what it does. It is the shape of a real performance ticket: measure, fix the biggest cost first, and prove the win.

Step 1 / 2Profile, then fix the cost
ⓘ Concept: The same discipline, applied end-to-end
The given top_terms is correct but slow: it calls list.count once per distinct token, re-scanning the whole list every time — O(n · u). Profile it to confirm the hot spot is the counting, not the sort; then count in a single pass (collections.Counter is O(n)) and sort the distinct tokens. Same result, a different complexity class.

Why it matters — A capstone is where separate skills become one workflow — the thing you actually do on the job.

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

1 graded rung · ~30 min

Profile a slow module, then optimise it under a metered budget — graded on correctness, speed, and approach (kernel, project).

Capstone — profile, then optimise to a budget (kernel, project)

Loading exercise…

That is the performance loop end-to-end: measure, fix the biggest cost, prove the win. It is exactly what a "make this faster" ticket asks for — now on evidence, not instinct.

Where next?

Continue

Project layout & pyproject

Project Layout & pyproject · P35