from line_profiler import LineProfiler
def do_profile(func):
def profiled_func(*args, **kwargs):
profiler = LineProfiler()
profiler.add_function(func)
profiler.enable_by_count()
try:
return func(*args, **kwargs)
finally:
profiler.disable_by_count()
profiler.print_stats()
sys.stdout.flush()
return profiled_func
用法:
@do_profile
def your_function():
// code
pass
输出类似于这样:
感觉很方便,调试的时候直接加上去就行