Performance Optimization
Profile first, then use better algorithms, built-ins, vectorisation or compiled code.
Intermediate
- Measure with
timeit,cProfile, Scalene or py-spy. - Fix the algorithm — beats any micro-tweak.
- Use built-ins and the standard library (implemented in C).
- Vectorise with NumPy instead of Python loops.
- 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.