Instead of only running the profile one time like this:
import cProfile
def do_heavy_lifting():
for i in range(100):
print('hello')
profiller = cProfile.Profile()
profiller.enable()
do_heavy_lifting()
profiller.disable()
profiller.print_stats(sort='time')
Where the profile results are like this:
502 function calls in 0.000 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
100 0.000 0.000 0.000 0.000 {built-in method builtins.print}
200 0.000 0.000 0.000 0.000 cp1252.py:18(encode)
200 0.000 0.000 0.000 0.000 {built-in method _codecs.charmap_encode}
1 0.000 0.000 0.000 0.000 test.py:2(do_heavy_lifting)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
I would like to run several times and print the average results, for better precision.
This is a initial script recipe I thought of:
import cProfile
def do_heavy_lifting():
for i in range(100):
print('hello')
def best_of_profillings(target_profile_function, count):
profile_results = []
for index in range(count):
profiller = cProfile.Profile()
profiller.enable()
target_profile_function()
profiller.disable()
profile_results.append(profiller)
profile_results /= count
return profile_results
heavy_lifting_result = best_of_profillings(do_heavy_lifting, 10)
heavy_lifting_result.print_stats(sort='time')
After running this, it should display the results like its first version did, but the difference is that they are the average of several runs, instead of running it one time.
The draft script still missing the part profile_results /= count
where after all the iterations, I would get all the computed results and create the average results and display it on the screen always:
502 function calls in 0.000 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
100 0.000 0.000 0.000 0.000 {built-in method builtins.print}
200 0.000 0.000 0.000 0.000 cp1252.py:18(encode)
200 0.000 0.000 0.000 0.000 {built-in method _codecs.charmap_encode}
1 0.000 0.000 0.000 0.000 test.py:2(do_heavy_lifting)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}