Skip to content

Instantly share code, notes, and snippets.

@pmelchior
Last active June 18, 2019 20:19
Show Gist options
  • Save pmelchior/99e559c86b1b68ca319bffba54d911a2 to your computer and use it in GitHub Desktop.
Save pmelchior/99e559c86b1b68ca319bffba54d911a2 to your computer and use it in GitHub Desktop.
Profile python code and plot inline chart
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')
@pmelchior
Copy link
Author

requires graphviz and gprof2dot; both available with pip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment