hammacktony
1/10/2020 - 5:43 AM

timing decorator

Timing Decorator

""" Simple timing decorator """
from functools import wraps
from time import time

__all__ = ("timer")

def timer(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        start = time()
        result = f(*args, **kwargs)
        total = time() - start
        print(f'func: {f.__name__} took: {total:2.4f} sec')
        return result
    return wrap

if __name__ == "__main__":
    """ Test timer function """

    @timer
    def test():
        import time
        time.sleep(2.5)

    test()