利用装饰器计算函数运行的时间

前端之家收集整理的这篇文章主要介绍了利用装饰器计算函数运行的时间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import time
from functools  wraps
def time_this_function(func):
    #作为装饰器使用,返回函数执行需要花费的时间
    @wraps(func)
    def wrapper(*args,**kwargs):
        start=time.time()
        result=func(*args,1)">kwargs)
        end=time.time()
        print("函数",func.__name__,运行时间:s")
        return result
     wrapper
if __name__=='__main__':
    @time_this_function
     count_number(n):
        while n>0:
            time.sleep(0.1)
            n+=-1
    count_number(10)
输出函数: count_number 运行时间: 1.0036 s

 

猜你在找的Python相关文章