Created
May 17, 2022 01:15
-
-
Save fanurs/55bf10e7098c3b1b8aa4e4c610d4e5c2 to your computer and use it in GitHub Desktop.
Python profiling (cProfile, pstats, snakeviz)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import pstats | |
with cProfile.Profile() as pr: | |
routine_to_measure() | |
stats = pstats.Stats(pr) | |
stats.sort_stats(pstats.SortKey.TIME) | |
# print profiling result | |
# Method 1 | |
stats.print_stats() | |
# Method 2 (better, but requires library "snakeviz") | |
# See: https://github.com/jiffyclub/snakeviz | |
stats.dump_stats(filename='result.prof') | |
# To view result on Linux: snakeviz result.prof | |
# A localhost will opened, showing profilng result on browser: | |
# snakeviz web server started on 127.0.0.1:8080; enter Ctrl-C to exit | |
# http://127.0.0.1:8080/snakeviz/user/fanurs/result.prof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment