10

I use cprofile to get high offenders, however the filename:lineno is only listing the filename, but having the filepath listed would be more usefull to quickly open that path. Especially if there might be same module nams in different hierarchies.

ncalls    tottime    percall    cumtime    percall    filename:lineno(function)
1         0.000       0.000       3.922    display.py:599 (show)

Is there an option to turn that into fullpath?

user1767754
  • 23,311
  • 18
  • 141
  • 164

3 Answers3

6

I guess you format the output with "pstats.Stats" class and you have:

stats = Stats(profiler)
stats.strip_dirs()  # remove this
user10916502
  • 61
  • 1
  • 2
3

If you are running from terminal, add -o flag:

python -m cProfile -o output.data your_script.py ...

Then from the console:

import pstats
ps = pstats.Stats('output.data')
ps.sort_stats(pstats.SortKey.CUMULATIVE).print_stats(20)

You should get an output like:

Tue Oct 19 07:17:56 2021    output.data

         18646457 function calls (18374990 primitive calls) in 30.760 seconds

   Ordered by: cumulative time
   List reduced from 22652 to 20 due to restriction <20>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   4321/1    0.037    0.000   30.796   30.796 {built-in method builtins.exec}
        1    0.000    0.000   30.796   30.796 manage.py:1(<module>)
      ...
marxin
  • 3,692
  • 3
  • 31
  • 44
2

There doesn't seem to be a built in way but you can do this:

import cProfile                                                    
import pstats   
                                                   
p = cProfile.Profile()                      
s = p.run("1+1")     
pstats.Stats(s).sort_stats(2).print_stats()
Timmmm
  • 88,195
  • 71
  • 364
  • 509