Measuring Function Running Times in Python

Measuring Function Running Times in Python

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Time in milliseconds

from datetime import datetime

def my_function():

    time_before = datetime.now()

    ## costly code..
    ##

    time_after = datetime.now()

    # returns a Delta object
    delta_time   = time_after - time_before
    milliseconds = int(delta.total_seconds() * 1000)

    print(f"Took {milliseconds}ms")

As a decorator

TODO