【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)

如果忍耐算是坚强 我选择抵抗 如果妥协算是努力 我选择争取
原文地址:https://www.cnblogs.com/danhuai/p/15330830.html