Python Profiling in Practice
Find repeated work, measure a change and check the result.

What you will build
Two implementations return the same 10,000 rows. The script saves output/slow.prof and output/indexed.prof. Elapsed times depend on your machine and are not a promised speedup.
This workbook is for readers who can run a Python script. It includes a small local example, explanations, exercises and worked answers. The package was checked on Windows with Python 3.13.17. Pinned package versions and setup instructions are included; they are not a claim to be the latest releases.
Read a sample
The following two sections are taken from the practical download edition.
Start with a question you can measure
A slow program gives you a symptom, not a diagnosis. Start by choosing one operation and a repeatable input. In this workbook, 10,000 orders must be matched to 1,000 customers. Each order has a customer ID, and the output contains the order ID and customer name. The data generator is deterministic, so a change in output cannot be explained away as different random input.
The first implementation scans the customer list for every order. It is deliberately simple. The second creates a dictionary once and looks up each customer ID in it. This is a useful profiling exercise because both functions implement the same result under explicit assumptions: customer IDs are unique, and every order refers to an existing customer.
Keep those assumptions visible. If duplicate customer IDs are possible, dict(customers) keeps the last value while the scan returns the first match. If IDs can be missing, the functions raise different exceptions. A fast answer that changes the business rule is not an optimisation.
Read the profile from the top down
The script creates a cProfile.Profile, calls runcall(function, customers, orders), then sorts its report by cumulative time. Cumulative time includes a function and the functions it calls. Total time reports time spent directly in a function, excluding its subcalls. Call counts help distinguish one expensive operation from a small operation repeated excessively.
Look for the scan and its generator expression near the top of the slow report. Then inspect why it runs for every order. Do not begin by rewriting the smallest helper just because its name looks familiar. The useful change here is to avoid repeated searching, not to shave syntax from the generator.
Profiling adds overhead. The printed seconds are useful diagnostic observations, but they are not a controlled benchmark. Small timings fluctuate with machine load, Python version, cache state and the profiler itself. Compare unprofiled runs separately if you need a performance claim.
Inside the complete guide
- Start with a question you can measure
- Read the profile from the top down
- Inspect a saved profile with SnakeViz
- Make one change and verify it
- Exercises and worked answers
Files in your download
The 5-page PDF includes the walkthrough and complete source listing. The examples folder contains:
profile_demo.pyREADME.txtrequirements.txt
Scope and source material
This is a focused learning project with synthetic example data. It does not include hosted services or production deployment. This revised edition is based on previously published articles and uses replacement runnable examples; it is not a verbatim reprint.
Get the guide on Payhip — £4.99