I am using cProfile
try to profile my codes:
pr = cProfile.Profile()
pr.enable()
my_func() # the code I want to profile
pr.disable()
pr.print_stat
You can run the profiler saving the output into a file, as you do:
import cProfile
cProfile.run('my_func()', 'profile_results')
And then format that result using the class pstats.Stats
(https://docs.python.org/3/library/profile.html#the-stats-class):
import pstats
file = open('formatted_profile.txt', 'w')
profile = pstats.Stats('.\profile_results', stream=file)
profile.sort_stats('cumulative') # Sorts the result according to the supplied criteria
profile.print_stats(15) # Prints the first 15 lines of the sorted report
file.close()