user3291939
user3291939

Reputation: 39

difference between two times

I am trying to get the difference between two times for performance testing of some code. First, i get the local time of the environment, Run the code to be perf tested and then get local time again. After that i subtract the time to see how long the test took. I am trying to be accurate up to minutes,seconds and microseconds.
I find interesting that this works if i set a print after each time i get the time:

startTest= datetime.datetime.now()
print startTest

i get:

TotalTime:  0:00:00.328000

However, it fails if i dont add any print statements after i get the local time, this is what im doing:

import datetime

startTest= datetime.datetime.now()
      #Code to be Tested#
endTest= datetime.datetime.now()
testTime = endTest - startTest
print 'TotalTime:  ',testTime 

This gives me the following output:

TotalTime:  0:00:00

any idea how to get it to print out on the format0:00:00.000000?

Upvotes: 0

Views: 190

Answers (2)

GVH
GVH

Reputation: 414

It prints the microseconds only if there is there is a nonzero value in the microseconds attribute of the the datetime.timedelta object.

If you want a different behavior, write your own string formatting function. Something like:

def delta_to_string(tdelta):
    return '%s days, %02d:%02d:%09.6f' % (
        tdelta.days,
        tdelta.seconds / 3600,
        (tdelta.seconds / 60) % 60,
        (tdelta.seconds % 60) + (tdelta.microseconds / 10**6.))

The %09.6f format means 9 digits total for a float including decimal, add leading 0s if necessary, 6 digits max for decimal portion. If you want more or fewer digits, you have to change both the total number of digits and the decimal part - 10 decimal places would be %013.10f.

This prints every part of the timedelta object. Let's try it:

>>> delta_to_string((datetime.datetime.now() - datetime.datetime.now()))
'0 days, 00:00:00.000000'

I think you wanted to preserve the functionality of not printing days if days is 0...

def delta_to_string(tdelta):
    return '%s%02d:%02d:%09.6f' % (
        ('%s days, ' % tdelta.days) if tdelta.days else '',
        tdelta.seconds / 3600,
        (tdelta.seconds / 60) % 60,
        (tdelta.seconds % 60) + (tdelta.microseconds / 10**6.))

Now the results are:

>>> s1 = datetime.datetime.now()
>>> s2 = datetime.datetime.now()
>>> delta_to_string(s1 - s2)
'-1 days, 23:59:57.387000'
>>> print (s1 - s2)
-1 day, 23:59:57.387000
>>> delta_to_string(s2 - s1)
'00:00:02.613000'
>>> print (s2 - s1)
0:00:02.613000
>>> delta_to_string(s2 - s2)
'00:00:00.000000'
>>> print (s2 - s2)
0:00:00

Doesn't behave quite the same way as the built in str function... I'll leave the differences as an "exercise for the reader."

Upvotes: 0

some_weired_user
some_weired_user

Reputation: 596

This does not directly answer your question, but you could have a look at Pythons timeit module http://docs.python.org/2/library/timeit.html which is for performance testing of small code snippets.

Upvotes: 1

Related Questions