Python · The language, not just syntax

How do you optimise Python code without wasting effort?

HardperformanceoptimizationalgorithmsUpdated Oct 2026
Quiz this track

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.

python
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]
Building a set once turns an O(n * m) nested scan into O(n + m), which usually dwarfs any interpreter-level tweak.

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.

Keep the optimised code readable and covered by tests. If a change makes the logic unrecognisable, document why it exists and add a benchmark that fails if the performance regresses.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How would you find the actual bottleneck in a service?
  2. 2When does caching hurt more than it helps?
  3. 3Why can vectorisation beat a Python loop by orders of magnitude?
  4. 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.