Skip to content

Measure the execution time of a function in Python using decorators

We can use decorators to calculate the execution time of a function.

from time import time

def performance(fn):
    def wrapper(*args, **kwargs):
        t1 = time()
        result = fn(*args, **kwargs)
        t2 = time()
        return result

    return wrapper

def sample_fn():
    for i in range(10000):
        i * 5



Here the output is in milliseconds.

See also  Combine two lists into a dictionary in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.