【Python】【装饰器】计算方法执行时间

 

 

import time


def wrapper_calc_time(print_log=True):
    """
    计算func执行时间
    :param print_log: 是否打印日志
    :return:
    """

    def wrapper(func):
        def inner_wrapper(*args, **kwargs):
            start_time = time.time()
            func_re = func(*args, **kwargs)
            tem_time = time.time() - start_time
            re_time = f'{func.__name__}耗时:{int(tem_time * 1000)}ms'
            if print_log:
                print(re_time)
            return func_re

        return inner_wrapper

    return wrapper


@wrapper_calc_time(print_log=True)
def aa(c, d):
    time.sleep(1)
    a = c / d


if __name__ == '__main__':
    aa(2, 1)

 

 

posted @ 2021-09-24 16:04  淡怀  阅读(152)  评论(0编辑  收藏  举报