188

What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing.

So far I have this code:

from datetime import datetime

tstart = datetime.now()
print t1

# code to speed test

tend = datetime.now()
print t2
# what am I missing?
# I'd like to print the time diff here
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

16 Answers16

252

datetime.timedelta is just the difference between two datetimes ... so it's like a period of time, in days / seconds / microseconds

>>> import datetime
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> c = b - a

>>> c
datetime.timedelta(0, 4, 316543)
>>> c.days
0
>>> c.seconds
4
>>> c.microseconds
316543

Be aware that c.microseconds only returns the microseconds portion of the timedelta! For timing purposes always use c.total_seconds().

You can do all sorts of maths with datetime.timedelta, eg:

>>> c / 10
datetime.timedelta(0, 0, 431654)

It might be more useful to look at CPU time instead of wallclock time though ... that's operating system dependant though ... under Unix-like systems, check out the 'time' command.

StevenWernerCS
  • 839
  • 9
  • 15
NickZoic
  • 7,575
  • 3
  • 25
  • 18
  • 1
    Anyone interested in getting total minutes can use `int(c.total_seconds() / 60)` in this case – sufinawaz Feb 06 '15 at 15:41
  • 3
    The [page for the timeit module](http://docs.python.org/library/timeit.html) says that the module "avoids a number of common traps for measuring execution times." Is this approach (using datetime.now) subject to any of those pitfalls? – kuzzooroo Feb 21 '15 at 20:55
  • @kuzzooroo yes, it is! It is would be better to use timeit instead of this method! For example, I have a test in a large python project that, when measured using this method results in a supposed 0.25s runtime. In reality, and according to timeit, the runtime of said function is actually 30 seconds! – kazaamjt Jun 02 '20 at 12:56
  • this is not usable for accurate timing since datetime is not a monotonic clock – phiresky Nov 23 '20 at 15:46
  • This appears to measure the wrong time if your Python program calls C++ code via CPython. – Felix Crazzolara Feb 13 '21 at 22:01
87

Since Python 2.7 there's the timedelta.total_seconds() method. So, to get the elapsed milliseconds:

>>> import datetime
>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now()
>>> delta = b - a
>>> print delta
0:00:05.077263
>>> int(delta.total_seconds() * 1000) # milliseconds
5077
Paolo
  • 20,112
  • 21
  • 72
  • 113
f.cipriani
  • 3,357
  • 2
  • 26
  • 22
38

You might want to use the timeit module instead.

Darius Bacon
  • 14,921
  • 5
  • 53
  • 53
32

I know this is late, but I actually really like using:

import time
start = time.time()

##### your timed code here ... #####

print "Process time: " + (time.time() - start)

time.time() gives you seconds since the epoch. Because this is a standardized time in seconds, you can simply subtract the start time from the end time to get the process time (in seconds). time.clock() is good for benchmarking, but I have found it kind of useless if you want to know how long your process took. For example, it's much more intuitive to say "my process takes 10 seconds" than it is to say "my process takes 10 processor clock units"

>>> start = time.time(); sum([each**8.3 for each in range(1,100000)]) ; print (time.time() - start)
3.4001404476250935e+45
0.0637760162354
>>> start = time.clock(); sum([each**8.3 for each in range(1,100000)]) ; print (time.clock() - start)
3.4001404476250935e+45
0.05

In the first example above, you are shown a time of 0.05 for time.clock() vs 0.06377 for time.time()

>>> start = time.clock(); time.sleep(1) ; print "process time: " + (time.clock() - start)
process time: 0.0
>>> start = time.time(); time.sleep(1) ; print "process time: " + (time.time() - start)
process time: 1.00111794472

In the second example, somehow the processor time shows "0" even though the process slept for a second. time.time() correctly shows a little more than 1 second.

mgoldwasser
  • 14,558
  • 15
  • 79
  • 103
24

You could also use:

import time

start = time.clock()
do_something()
end = time.clock()
print "%.2gs" % (end-start)

Or you could use the python profilers.

Zitrax
  • 19,036
  • 20
  • 88
  • 110
  • 4
    When using `start = time.clock()` it prints `DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead`. – Contango Jul 21 '20 at 20:02
6

The following code should display the time detla...

from datetime import datetime

tstart = datetime.now()

# code to speed test

tend = datetime.now()
print tend - tstart
killy971
  • 650
  • 6
  • 16
4

You could simply print the difference:

print tend - tstart
sth
  • 222,467
  • 53
  • 283
  • 367
4

You may want to look into the profile modules. You'll get a better read out of where your slowdowns are, and much of your work will be full-on automated.

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
3

Arrow: Better dates & times for Python

import arrow
start_time = arrow.utcnow()
end_time = arrow.utcnow()
(end_time - start_time).total_seconds()  # senconds
(end_time - start_time).total_seconds() * 1000  # milliseconds
Eds_k
  • 944
  • 10
  • 12
3

You need to use time.time() instead, which outputs unix time with high precision.

Use this code:

from time import time

tstart = time()
doSomething()
tend = time()
difference = tend - tstart
print("The doSomething function took {} seconds to execute".format(difference))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lucas Urban
  • 627
  • 4
  • 15
3

I am not a Python programmer, but I do know how to use Google and here's what I found: you use the "-" operator. To complete your code:

from datetime import datetime

tstart = datetime.now()

# code to speed test

tend = datetime.now()
print tend - tstart

Additionally, it looks like you can use the strftime() function to format the timespan calculation in order to render the time however makes you happy.

Mike C.
  • 4,917
  • 8
  • 34
  • 38
2

Here is a custom function that mimic's Matlab's/Octave's tic toc functions.

Example of use:

time_var = time_me(); # get a variable with the current timestamp

... run operation ...

time_me(time_var); # print the time difference (e.g. '5 seconds 821.12314 ms')

Function :

def time_me(*arg):
    if len(arg) != 0: 
        elapsedTime = time.time() - arg[0];
        #print(elapsedTime);
        hours = math.floor(elapsedTime / (60*60))
        elapsedTime = elapsedTime - hours * (60*60);
        minutes = math.floor(elapsedTime / 60)
        elapsedTime = elapsedTime - minutes * (60);
        seconds = math.floor(elapsedTime);
        elapsedTime = elapsedTime - seconds;
        ms = elapsedTime * 1000;
        if(hours != 0):
            print ("%d hours %d minutes %d seconds" % (hours, minutes, seconds)) 
        elif(minutes != 0):
            print ("%d minutes %d seconds" % (minutes, seconds))
        else :
            print ("%d seconds %f ms" % (seconds, ms))
    else:
        #print ('does not exist. here you go.');
        return time.time()
Ulad Kasach
  • 11,558
  • 11
  • 61
  • 87
  • Traceback (most recent call last): File "redis-get-response.py", line 22, in time_var = time_me(); # get a variable with the current timestamp File "redis-get-response.py", line 20, in time_me return time.time() NameError: name 'time' is not defined – Joe Jan 29 '19 at 21:36
2

time.time() / datetime is good for quick use, but is not always 100% precise. For that reason, I like to use one of the std lib profilers (especially hotshot) to find out what's what.

nilamo
  • 1,932
  • 13
  • 22
0

You could use timeit like this to test a script named module.py

$ python -mtimeit -s 'import module'
Dhiraj Thakur
  • 716
  • 10
  • 25
0
start = datetime.now() 

#code for which response time need to be measured.

end = datetime.now()
dif = end - start
dif_micro = dif.microseconds # time in microseconds
dif_millis = dif.microseconds / 1000 # time in millisseconds
Basant Kumar
  • 129
  • 1
  • 3
  • 2
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – Amit Verma Feb 24 '21 at 07:37
0

In case anyone needs something like this to analyze delays between log entries for example ... etc.

def get_time_diff_between_timestruct_tuples(timestruct_tuples):
    """
    expecting input like:
    [(0, datetime.datetime(2021, 10, 27, 16, 6, 8, 590892)),
    (1, datetime.datetime(2021, 10, 27, 16, 6, 8, 591833)),
    (2, datetime.datetime(2021, 10, 27, 16, 6, 9, 434053)),
    (3, datetime.datetime(2021, 10, 27, 16, 6, 9, 878021)), ...]
    
    output like:
    [0.941, 0.84222, 0.443968, ...]
    """
    
    def seconds_mms_diff(t0, t1):
        diff = t1 - t0
        s = diff.seconds
        mms = diff.microseconds
        return float(f"{s}.{mms}")
    
    timediffs = []
    init = timestruct_tuples[0][1]
    idx = 0
    while idx < (len(timestruct_tuples)-1):
        timediffs.append(seconds_mms_diff(init, timestruct_tuples[idx+1][1]))
        idx += 1
        init = timestruct_tuples[idx][1]
    return timediffs
Goran B.
  • 542
  • 4
  • 14