Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python cProfile/snakeviz can't handle multiple functions calling same function

I'm having issues interpreting cProfile data. To show you my problem I created this simple script.

The function D calls B and C, which both call A.
Function A clearly takes up 1 sec (+overhead).
If we look at the snakeviz results then you can see that the reporting is a bit weird. I understand that in total 2 sec has been spent by function A, but inside function C, function A has spent only 1 sec, and that's what I am interested in. Does anybody know if there is a setting (or a different viewer) where I do not have this issue?

import time
import cProfile

def A():
    time.sleep(1)

def B():
    A()

def C():
    A()

def D():
    B()
    C()

cProfile.run('D()','profileResults.prf')

snakeviz results

like image 589
Matthias Baert Avatar asked Oct 19 '25 07:10

Matthias Baert


1 Answers

Unfortunately, the Python profile does not store the entire call tree. (This would be too expensive.) I've documented the problem here and as a snakeviz issue.

I recently created tuna for visualizing Python profiles to work around some of these problems. tuna cannot show the entire call tree, but at least it doesn't show wrong info either.

Install with

pip3 install tuna

Create a runtime profile

python -m cProfile -o program.prof yourfile.py

and just run tuna on the file

tuna program.prof

enter image description here

like image 163
Nico Schlömer Avatar answered Oct 20 '25 21:10

Nico Schlömer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!