加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 09:52:08 所属栏目:Python 来源:网络整理
导读: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 ( " 函数: "
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

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读