Discuss / Python / 交作业。。。计算函数运行的装饰器

交作业。。。计算函数运行的装饰器

Topic source

import functools, time

def metric(fn):

    @functools.wraps(fn)

    def wrapper(*args):

        start = time.time()

        r = fn(*args)

        end = time.time()

        cost = (end - start) * 1000

        print('%s executed in %s ms' % (fn.__name__, str(round(cost, 2))))

        return r

    return wrapper

@metric

def test():

    s = sum(range(1000000))

    print(s)

    return s

test()


  • 1

Reply