Aller au contenu

How to Python Profiling#

What to add in your code#

Python
import datetime
import cProfile
import pstats

with cProfile.Profile() as pr :

    function_to_measure()

# Print stats
stats = pstats.Stats(pr)
stats.sort_stats(pstats.SortKey.TIME)
stats.print_stats()
# Save profiler File
stats.dump_stats(filename="needs_profiling.prof")

View Profiler File#

Bash
1
2
3
pip install snakeviz

snakeviz needs_profiling.prof