Python ist langsam? Vielleicht. Aber messen Sie zuerst, wo.
cProfile¶
python -m cProfile -s cumulative app.py
snakeviz¶
python -m cProfile -o profile.prof app.py
snakeviz profile.prof
line_profiler¶
@profile
def slow_function(): …
kernprof -l -v script.py¶
memory_profiler¶
python -m memory_profiler script.py
py-spy – Sampling ohne Overhead¶
py-spy record -o profile.svg – python app.py
Optimierung¶
- Generators statt Lists
- dict/set fuer Lookups
- functools.lru_cache
- numpy fuer Numerik
- multiprocessing fuer CPU-bound
- asyncio fuer I/O-bound
Workflow¶
cProfile -> snakeviz -> line_profiler -> Optimierung -> erneut messen.
pythonperformanceprofiling