首页 > 其他 > 详细

计算代码运行时间--带有返回值的装饰器

时间:2021-04-02 22:07:21      阅读:24      评论:0      收藏:0      [点我收藏+]

示例代码

技术分享图片
import time


def cal_time(fn):
    """计算程序运行时间的函数"""
    def inner():
        start_time = time.time()
        ret = fn()
        end_time = time.time()
        print("程序一共执行了%s" % (end_time - start_time) + )
        return ret
    return inner


@cal_time
def func():
    """带有返回值的求阶乘的函数"""
    x = 1
    for i in range(1, 100000):
        x *= i
    print(x)
    return x


result = func()  # 相当于 func = cal_time(func)
print(result) # 查看inner函数的返回值
View Code

运行结果:

技术分享图片

 

计算代码运行时间--带有返回值的装饰器

原文:https://www.cnblogs.com/yujiemeigui/p/14612158.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!