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
输出类似于这样:

感觉很方便,调试的时候直接加上去就行
文章介绍了如何使用LineProfiler装饰器在Python代码中进行功能性性能分析,通过添加、启用和禁用Profiler,实现在运行时查看函数调用的详细统计信息。

247

被折叠的 条评论
为什么被折叠?



