4

My question is very similar to that of Brice Thomas : Python getting meaningful results from cProfile However I could not find a satisfying answer. I'll explain :

I get similar results, ie the majority of the time is spent on the functions I use (prox.py, rawprox.py and position.py).

I could optimize the time spent on other operations but now I really want to understand what takes the most time within my functions.

I followed arbarnert's answer but it looks like it only works because the whole code is written between pr.enable() and pr.disable(). I mean that the cProfile results are limited to the functions written between those lines. This means that to make it work, I should not call any of the functions I want to have details of even a single time, because I don't want these functions to appear in the cProfile results. I want their subfunctions to appear.

Example :

import cProfile
pr = profile.Profile()
pr.enable()
any_operations_without_my_functions_called
pr.disable()
pr.print_stats(sort='time')

will work and tell me what operations take the most time.

Contrarily,

import cProfile
pr = profile.Profile()
pr.enable()
call_prox.py
call_rawprox.py
call_position.py
pr.disable()
pr.print_stats(sort='time')

will only result in telling me that prox.py took x seconds, rawprox.py took y seconds and position.py took z seconds, while I want to know what operations took the most time within them.

I clearly do not want to report the whole code inside these functions between the profiler lines, because the functions are called inside loops and it would be an incredible mess (actually, it would be totally undoable and I believe someone must have thought of a way of doing this).

Is there any solution you would recommend?

Ashargin
  • 498
  • 4
  • 11
  • Write a script that performs the same process as a function you are curious about then profile it? – wwii Jul 06 '17 at 16:41
  • 2
    You want line_profiler... https://pypi.python.org/pypi/line_profiler/ - and also, if you are going to do a lot of profiling, it's a little easier in ipython using its %prun and %lprun magics... – Corley Brigman Jul 06 '17 at 17:08
  • @wwii that's what i did but here are more details : I have a script which uses these functions I want details of, and running this script through cProfile will not give me the time spent on subfunctions inside these functions – Ashargin Jul 06 '17 at 17:27
  • @CorleyBrigman I had heard about this once and this may just be what I'm looking for. Thanks in advance, I'll try this out. – Ashargin Jul 06 '17 at 17:27

0 Answers0