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

5年以前  |  阅读数:452 次  |  编程语言:Python 
def decorator_timer(foo):
        def wrapper(*arg, **karg):
            t1 = time.time()
            result = foo(*arg, **karg)
            t2 = time.time()
            print(pre+"time:", t2 - t1)
            return result
        return wrapper

@decorator_timer("@_@")
def my_power(a, b):
    return a**b

print(my_power(2,3))
#输出:
#time: 0.0
#5

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8