Question
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 thetimedelta 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
datetime.timedelta is just the difference between two datetimes ... so it's like a period of time, in days / seconds / microseconds:
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:
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.
沒有留言:
張貼留言