Find the hot spot first

P34.profiling.01 · Audience: guest, it-ml, language-pro

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

The first rule of making Python fast: don't guess where the time goes — measure. Intuition about hot spots is wrong more often than right, and optimising the function you think is slow is wasted effort if the time is really spent elsewhere. This module points the standard-library profiler at your code so the profile, not your hunch, tells you what to fix.

Step 1 / 3Measure before you optimise
ⓘ Concept: The profile beats the hunch
cProfile runs your code and records, per function, how many times it was called and how long it took. You enable() it, run the workload, disable(), and read the result — evidence instead of instinct. The motto of this whole pillar: always profile before you optimise.

Why it matters — Optimising the wrong function is the most common way to spend a day and gain nothing — the profiler stops that before it starts.

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

Use cProfile to find the busiest function in a workload, then argue an optimisation approach in an interview-style prompt.

Rung 1 — profile and find the hot spot (cProfile)

Loading exercise…

Rung 2 — measure before you optimise (interview)

Loading exercise…

Once the profile tells you where the time goes, the next question is often memory, not speed — which is what tracemalloc measures in the next module.

Where next?