How to profile Python code for performance

· Category: Python Programming

Short answer

Use cProfile for function-level timing, line_profiler for line-by-line analysis, and memory_profiler for memory usage. Focus on optimizing the hottest paths. For parallelism to improve performance, see how to use python multiprocessing module. For understanding concurrency limits, see what is the python global interpreter lock.

Steps

  1. Run cProfile: python -m cProfile -s cumtime script.py
  2. Identify the top time-consuming functions
  3. Install line_profiler: kernprof -l -v script.py
  4. Check memory with memory_profiler: @profile decorator
  5. Optimize the bottleneck and measure again

Tips

  • Always measure before optimizing to avoid premature optimization
  • Use timeit for microbenchmarks of small code snippets
  • For asynchronous patterns, see python asyncio