Python, from Zero, for AILesson 69 of 89

Module 7 · Objects, generators and the shape a larger program takes

Profiling: finding where the time actually goes before you optimise anything

In this lesson

  • 9 min read
  • 8 reading sections
  • Knowledge check
  • Lesson 10 of 10 in this chapter

This chapter’s goalRestructure a growing script into classes and dataclasses with a swappable provider behind one interface, implement the dunder methods that let your objects work with for, len, in and with, write a decorator that retries or caches, run a type checker and act on its output, make the project installable as a command with pyproject.toml, and profile it to say from measurement rather than guesswork where the time goes.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:40Not available
Full audiobookNot availableNot available
Where fourteen minutes went in a tagging scriptWaiting for the model API91Parsing the JSON replies4Reading and writing theCSV4The loop everyone wantedto rewrite1% of total run timeRewriting the loop perfectly saves about eight seconds. Caching repeated prompts and overlapping thecalls saves twelve minutes. Nobody can tell these apart by reading the code, which is the entireargument for putting a stopwatch around three suspects before changing anything.
Rewriting the loop perfectly saves about eight seconds. Caching repeated prompts and overlapping the calls saves twelve minutes. Nobody can tell these apart by reading the code, which is the entire argument for putting a stopwatch around three suspects before changing anything.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 10Free · no account needed
Lesson steps

Guessing is wrong more often than not

Every programmer has a story of optimising the wrong thing. The loop that looked slow was 0.3 per cent of the run; the innocent-looking line that formatted a date was called two million times. Intuition about performance is poor even in experienced people, because the cost of an operation is invisible in the source. The remedy is to measure, and Python ships the instruments.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Profiling: finding where the time actually goes before you optimise anything’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/profiling-where-time-goes (Accessed: 21 November 2026).