MehmedB
MehmedB

Reputation: 1137

What is the best way to get the execution time spent of a single line of code?

I am trying to get the execution time spent on a single line of code. Doing this for multiple lines and/or writing code every time like in the example below is just frustrating. Is there a better way to do this? See the example below.

import pandas as pd
import datetime

start_time = datetime.datetime.now()
df = pd.read_csv('dummy.csv')
print('Time spent reading: {}'.format(datetime.datetime.now() - start_time))
start_time = datetime.datetime.now()
df.head(n=100)
print('Time spent printing top 100: {}'.format(datetime.datetime.now() - start_time)) 

Upvotes: 1

Views: 2425

Answers (2)

Thomas
Thomas

Reputation: 721

Python has variadic arguments (as well as keyword arguments), argument defaults, and has first-class functions. Basically this means you can pass in a function and optionally all of its arguments and keyword arguments into another function.

from datetime import datetime

def record_time(task, args=(), kwargs={}):
    start_time = datetime.now()
    task(*args, **kwargs)

    return datetime.now() - start_time

print(record_time(print, ("test", )))

Just remember that a 1-ary tuple needs to be written as ('foo', ) and not as ('foo').


You can also use context managers that will allow you to time blocks of code rather than just individual functions.

import contextlib

from datetime import datetime
from time import sleep

@contextlib.contextmanager
def record_time():
    try:
        start_time = datetime.now()
        yield
    finally:
        print(datetime.now() - start_time)

with record_time():
    # Do a lot of stuff here...
    sleep(2)
    # Do more stuff if you like.

with record_time(): sleep(2)

By nature, it is impossible to actually return the time this way but printing it isn't hard.

Upvotes: 3

user11988675
user11988675

Reputation:

You could use the module timeit

Example use

import timeit

timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

Out[1]: 0.25778120000006766

Thos code worked fine when using python 3.

You can run multiple cycles to be sure of any variability, and of course you can substitute the code between the ' ' with your own oneliner then.

More information can be found on the doc page for timeit

Upvotes: 1

Related Questions