Python · The language, not just syntax
How do you optimise Python code without wasting effort?
Short answer
Measure the real workload first, fix algorithmic and data-structure problems, then reduce I/O and repeated work. Micro-optimising before profiling usually adds complexity without moving the bottleneck.
Performance work starts with a baseline and a target. Profile the actual request or job, not a synthetic loop, and identify whether time goes to CPU, I/O, allocation or waiting on a dependency. The largest win is usually an algorithmic or architectural change.
def find_missing(orders, shipped):
shipped_ids = {order["id"] for order in shipped}
return [order for order in orders if order["id"] not in shipped_ids]Optimisation order
- Algorithm and data structure: the right complexity beats faster syntax
- I/O: batch queries, reuse connections, stream instead of loading everything
- Repeated work: cache pure computations with a clear invalidation policy
- Vectorisation: use NumPy or Polars instead of Python loops over numeric data
- Concurrency: threads for blocking I/O, processes for CPU, asyncio for high-fanout I/O
Caching is not free. It trades memory and correctness risk for speed, and a stale cache can be worse than a slow response. Set bounds, expiry and invalidation deliberately, and measure the hit rate.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1How would you find the actual bottleneck in a service?
- 2When does caching hurt more than it helps?
- 3Why can vectorisation beat a Python loop by orders of magnitude?
- 4How do you prevent a performance regression from returning?
Say it in the interview
I start by profiling the real workload and setting a target, because intuition about bottlenecks is often wrong. The biggest wins usually come from complexity, data structures and I/O batching rather than micro-tweaks. After that I consider caching, vectorisation and the right concurrency model. I keep a benchmark and tests so the optimisation can be maintained and regressions are caught.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.