Skip to content

Performance and Profiling

Beginning of the lesson

Performance and Profiling

Measure first, optimise the hotspot, then measure again

Big Idea: Fast code is not written by guessing. You measure where the time actually goes with tools like timeit and cProfile, you reason about how cost grows with input size (big-O), and only then do you change the part that matters — usually by picking a better algorithm or data structure, caching repeated work, or pushing tight loops into vectorised operations. The rest of the code you leave clear and simple.
By the end you will be able to:
  • time a small snippet correctly with timeit and avoid its common traps,
  • profile a whole program with cProfile and read a pstats report,
  • read big-O growth and connect it to real wall-clock time,
  • replace an O(n) membership test with an O(1) one using set/dict,
  • cache repeated work with functools.lru_cache,
  • recognise when to vectorise a hot loop instead of hand-writing it.

1. Measure, do not guess

The first rule of performance work is that programmer intuition about "what is slow" is usually wrong. The interpreter, the operating system, memory caches and library internals all interact in ways that are hard to predict by reading code. So the professional workflow is a loop: measure -> find the hotspot -> change one thing -> measure again.

A related rule: optimise only what matters. A function that runs once at start-up does not deserve the attention that a function called ten million times inside a loop does. Donald Knuth's famous line — "premature optimization is the root of all evil" — is really about spending effort where it does not pay. Clear code is the default; you trade clarity for speed only on the proven hotspot.

Two kinds of measurement answer two different questions. A microbenchmark (timeit) answers "which of these two small snippets is faster?". A profiler (cProfile) answers "in this whole program, where does the time actually go?". Reach for the right one.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers