编程语言
首页 > 编程语言> > Python 函数运行时间统计

Python 函数运行时间统计

作者:互联网

from functools import wraps
import time


def func_time(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = f(*args, **kwargs)
        end = time.time()

        print(f"\033[1;31;40m{f.__name__} took {(end-start):.3f}s")
        return result
    return wrapper

@func_time
def test_1():
    time.sleep(1)

@func_time
def test_2():
    time.sleep(0.5)
    test_1()

test_2()

"""
test_1 took 1.013s
test_2 took 1.519s
"""

  

标签:函数,Python,took,统计,func,time,test,def,result
来源: https://www.cnblogs.com/tynam/p/16587678.html