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
- Run cProfile:
python -m cProfile -s cumtime script.py - Identify the top time-consuming functions
- Install line_profiler:
kernprof -l -v script.py - Check memory with
memory_profiler:@profiledecorator - Optimize the bottleneck and measure again
Tips
- Always measure before optimizing to avoid premature optimization
- Use
timeitfor microbenchmarks of small code snippets - For asynchronous patterns, see python asyncio