This looks like a bug or limitation. Nested profilers do not work. here is a repro:
import cProfile
import pstats
import time
import cStringIO as StringIO
def do():
for i in range(100):
time.sleep(0.01)
def do_more():
for i in range(100):
time.sleep(0.01)
def do_all():
all_profiler = cProfile.Profile()
all_profiler.enable()
profiler = cProfile.Profile()
profiler.enable()
do()
profiler.disable()
strio = StringIO.StringIO()
stats = pstats.Stats(profiler, stream=strio)
stats.sort_stats("cumulative")
stats.print_stats(10)
print "do profile:\n{}".format(strio.getvalue())
do_more()
all_profiler.disable()
strio = StringIO.StringIO()
stats = pstats.Stats(all_profiler, stream=strio)
stats.sort_stats("cumulative")
stats.print_stats(10)
print "all profile:\n{}".format(strio.getvalue())
do_all()
Added to tmp.py and run in the shell, output is:
do profile:
103 function calls in 1.009 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.009 1.009 tmp.py:6(do)
100 1.008 0.010 1.008 0.010 {time.sleep}
1 0.000 0.000 0.000 0.000 {range}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
all profile:
1 function calls in 2.018 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 2.018 2.018 2.018 2.018 {method 'enable' of '_lsprof.Profiler' objects}
Note the "all_profiler" only shows the other profiler.
EDIT: it is possible this is only an issue with the summary printing. I didn't try dumping the stats to use with a profile viewer like converting for use with kCachegrind, for example.
return
instead ofyield
, the output file became useful – Seneschal