python,统计函数执行时间装饰器

import time
from functools import wraps

def timethis(func):
    '''
    Decorator that reports the execution time.
    '''
    @wraps(func)
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(func.__name__, end-start)
        return result
    return wrapper

@timethis
def countdown(n):

    while n > 0:
        n -= 1

countdown(10000000)

 

posted @ 2021-11-18 16:15  米开朗菠萝  阅读(110)  评论(0)    收藏  举报