Matthias Baert
Matthias Baert

Reputation: 51

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

Upvotes: 4

Views: 2698

Answers (1)

Nico Schlömer
Nico Schlömer

Reputation: 58781

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

Upvotes: 7

Related Questions