python 代码性能分析

pip install line_profiler

 

def profile(func):
    from functools import wraps

    @wraps(func)
    def wrapper(*args, **kwargs):
        from line_profiler import LineProfiler
        prof = LineProfiler()
        try:
            return prof(func)(*args, **kwargs)
        finally:
            prof.print_stats()

    return wrapper

 

使用方式:

在要测试的函数上加装饰器

@profile
def funx():
    xxx

 

posted @ 2021-08-04 10:32  谷阿幻  阅读(56)  评论(0编辑  收藏  举报