Skip to content

Instantly share code, notes, and snippets.

@fanurs
Created May 17, 2022 01:15
Show Gist options
  • Save fanurs/55bf10e7098c3b1b8aa4e4c610d4e5c2 to your computer and use it in GitHub Desktop.
Save fanurs/55bf10e7098c3b1b8aa4e4c610d4e5c2 to your computer and use it in GitHub Desktop.
Python profiling (cProfile, pstats, snakeviz)
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