Skip to the solution
MediumBackend14 min read

How do you measure and profile Python performance?

895 views

The problem

Question presented to candidate: "A report endpoint written in Python has become slow, and a colleague wants to rewrite half of it. Before anyone changes code: how would you measure how slow it is, find out where the time and memory actually go, and prove that a fix helped?"

What a strong answer should cover:

  • Measure before optimising: a repeatable benchmark, timed with time.perf_counter() for a block or timeit (many loops, several repeats, take the minimum) for small statements.
  • Profile to find the hot spot: cProfile (a deterministic profiler, python -m cProfile -s cumulative app.py) and reading pstats columns: ncalls (with total/primitive for recursion), tottime vs cumtime.
  • Know the profiler families and their overhead: deterministic (cProfile) vs sampling profilers (py-spy, and profiling.sampling in the standard library since Python 3.15) that can attach to a running process with little slowdown; line-level tools such as line_profiler.
  • Memory is separate: sys.getsizeof is shallow, tracemalloc records allocations, peaks and the source lines responsible.
  • Fix the algorithm or data structure first (a set instead of a list for membership, caching with lru_cache, a generator instead of a giant list), then re-measure the same benchmark.
  • Caveats: noisy timings, warm-up, profiler overhead distorting tiny functions, and measuring on realistic data.

Clarifying questions expected:

  • Is the problem latency of one request, throughput, or memory?
  • Can we reproduce it locally with realistic data, or only in production (which favours a sampling profiler attached to the live process)?

Code / implementation expected: Optional โ€” a timeit comparison or a cProfile run and how to read its table.

  • performance
  • timeit
  • cprofile

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

14 min read ยท 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

Pythonperf_counter, timeit.repeat and autorange: list vs set membership
PythoncProfile and pstats: exact call counts, recursion and cumulative time
PythonMemory: sys.getsizeof vs tracemalloc peaks and snapshots

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 59 of 139 in Python

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Python questions
Up nextMedium

What is the LEGB rule for scope resolution?

Continue