Decorator for timeit.timeit method?

前端 未结 1 1055
情书的邮戳
情书的邮戳 2021-02-11 01:23

I\'m trying to write a simple time decorator to measure time taken by functions. However the code below is giving our recursion error. What\'s wrong with it?

imp         


        
1条回答
  •  执念已碎
    2021-02-11 01:45

    from functools import wraps
    from time import time
    def measure(func):
        @wraps(func)
        def _time_it(*args, **kwargs):
            start = int(round(time() * 1000))
            try:
                return func(*args, **kwargs)
            finally:
                end_ = int(round(time() * 1000)) - start
                print(f"Total execution time: {end_ if end_ > 0 else 0} ms")
        return _time_it
    @measure
    def hello():
        print('hello world')
    
    hello()
    

    0 讨论(0)
提交回复
热议问题