I am using cProfile try to profile my codes:
cProfile
pr = cProfile.Profile() pr.enable() my_func() # the code I want to profile pr.disable() pr.print_stat
You don't really need StringIO, as a file qualifies as a stream.
import pstats with open("profilingStatsAsText.txt", "w") as f: ps = pstats.Stats("profilingResults.cprof", stream=f) ps.sort_stats('cumulative') ps.print_stats()