python装饰器统计各模块耗时

2019-05-22  本文已影响0人  Han筱希

此方法不改变被装饰函数的返回值

import datetime
import functools
import time

def time_me(func):
    '''
    @summary: cal the time of the fucntion
    @param : None
    @return: return the res of the func
    '''
    def wrapper(*args,**kw):
        start_time = datetime.datetime.now()
        res = func(*args,**kw)
        over_time = datetime.datetime.now()
        print ('current Function {0} run time is {1}'.format(func.__name__ , (over_time - start_time).total_seconds()))
        return res
    return wrapper

@time_me
def test1():
    time.sleep(1)
    return 'aaa'

A = test1()
print A

执行结果:

current Function test1 run time is 1.00057
aaa
上一篇 下一篇

猜你喜欢

热点阅读