def profile(sourcecode, p_locals, p_globals):
import cProfile
import pstats
prof_filename = os.path.join(tempfile.gettempdir(), "%s.prof" % __file__)
try:
cProfile.runctx(sourcecode, p_locals, p_globals, prof_filename)
p = pstats.Stats(prof_filename)
p.sort_stats('cumulative').print_stats(40)
finally:
os.remove(prof_filename)
评论列表
文章目录