Skip to content
UPEEC

Performance Optimization

Profile first, then use better algorithms, built-ins, vectorisation or compiled code.

Intermediate
Comments 0
  1. Measure with timeit, cProfile, Scalene or py-spy.
  2. Fix the algorithm — O(n2)→O(nlog⁡n)O(n^2) \to O(n \log n) beats any micro-tweak.
  3. Use built-ins and the standard library (implemented in C).
  4. Vectorise with NumPy instead of Python loops.
  5. Compile hot spots with Numba, Cython or mypyc; cache results with functools.lru_cache.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.