Python decorator to measure the execution time of methods

PHOTO EMBED

Tue Dec 14 2021 08:31:00 GMT+0000 (Coordinated Universal Time)

Saved by [deleted user]

def timeit(method):
    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()

        print(f'{method.__name__}  {(te - ts):.2f} s')

        return result
    return timed
content_copyCOPY

https://medium.com/pythonhive/python-decorator-to-measure-the-execution-time-of-methods-fa04cb6bb36d