Last active
June 18, 2019 20:19
-
-
Save pmelchior/99e559c86b1b68ca319bffba54d911a2 to your computer and use it in GitHub Desktop.
Profile python code and plot inline chart
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, os, tempfile | |
from IPython.display import Image | |
def cprof2png(command): | |
# create tmp file to store cprof | |
temp = tempfile.NamedTemporaryFile() | |
# run profiler | |
cProfile.run(command, temp.name) | |
# parse to dot and make png figure from temp | |
os.system(f"gprof2dot -f pstats {temp.name} | dot -Tpng -o {temp.name}") | |
return Image(f"{temp.name}", format='png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires graphviz and gprof2dot; both available with pip