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
You could also use:
Or you could use the python profilers.
Here is a custom function that mimic's Matlab's/Octave's
tic
toc
functions.Example of use:
Function :
Since Python 2.7 there's the timedelta.total_seconds() method. So, to get the elapsed milliseconds:
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.
You could use timeit like this to test a script named module.py
You could simply print the difference: