Execution time decorator

PHOTO EMBED

Mon Jan 31 2022 02:19:07 GMT+0000 (UTC)

Saved by @aguest #python #decorator

from time import time


def measure_time(func):
    def wrapper(*args, **kwargs):
        start = time()
        result = func(*args, **kwargs)
        print(f'Elapsed time is {time() - start} ms')
        return result
    return wrapper
content_copyCOPY

## Usage example ```python import time @measure_time def sleeping_func(sleep_time): time.sleep(sleep_time) sleeping_func(0.5) sleeping_func(1) sleeping_func(1.5) sleeping_func(2) ``` ## Output Elapsed time is: 0.5003328323364258 ms Elapsed time is: 1.0017611980438232 ms Elapsed time is: 1.5045099258422852 ms Elapsed time is: 2.003535747528076 ms