def train_with_profiling(opts, dirs):
import cProfile, pstats, StringIO
cProfile.runctx('train(opts, dirs)', \
{'train': train, 'opts': opts, 'dirs': dirs},
{}, 'mainstats')
# create a stream for the profiler to write to
profiling_output = StringIO.StringIO()
p = pstats.Stats('mainstats', stream=profiling_output)
# print stats to that stream
# here we just report the top 30 functions, sorted by total amount of time spent in each
p.strip_dirs().sort_stats('cumulative').print_stats(30)
# print the result to the log
print('---Profiling result follows---\n%s' % profiling_output.getvalue() )
profiling_output.close()
print('---End of profiling result---')
评论列表
文章目录