python code profile
import os
import cProfile
import pstats
# gprof2dot must be installed
# dot must be installed
def profile_function(function_call: str, sort_stat='tottime'):
name_file = function_call.split('(')[0]
cProfile.run(function_call, f'code_profiling/{name_file}.dat')
with open(f'code_profiling/{name_file}.txt', 'w') as f:
p = pstats.Stats(f'code_profiling/{name_file}.dat', stream=f)
p.sort_stats(sort_stat).print_stats()
com = f'gprof2dot -f pstats code_profiling/{name_file}.dat | dot -Tpng -o code_profiling/{name_file}.png'
os.system(com)
function_call = "any_function(arg_1, arg_2)"
profile_function(function_call)