I have essentially done the following:
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
# ... my code did something ...
pr.disable()
s = Str
You can try snakeviz
https://jiffyclub.github.io/snakeviz
It is a browser based graphical viewer for the output of Python’s cProfile module and an alternative to using the standard library pstats module.
# to install it with pip
pip install snakeviz
# once installed, you can use snakeviz to view the file
snakeviz /path/to/your/dump/pstat/file
Here is a sample image for a visualized pstat file you can dump like above.