0

I am trying to count how long a very long process is taking:

import datetime

def main(argv):
    starttime = datetime.datetime.now()
    for f in somearray:
        doSomething(f)
    endtime = datetime.datetime.now()
    deltatime = endtime-starttime
    print "Operation took " + str(deltatime.seconds) + " seconds"

def doSomething(f):
    # this takes a looong time (~10 minutes)

In the code above I only end up getting the time elapsed for the last time doSomething was run. I used to have doSomething as part of the main function and the timing was fine, but it made sense to move it to its own function.

I saw this question but it seems to serve a different need.

What am I doing wrong?

Thanks

Community
  • 1
  • 1
mga
  • 1,960
  • 1
  • 23
  • 31
  • So you actually want to know how long it took to execute doSomethin once? Why don't you put it inside the method? – fiscblog Aug 15 '13 at 14:53
  • I want to know how long all of it took to execute (`f` occurrences of `doSomething`) – mga Aug 15 '13 at 15:05

1 Answers1

0

What is the error you are getting? datetime doesn't have a .seconds attribute but it does have a .second attribute. maybe try str(deltatime.second) in your code.

  • 1
    His `deltatime` is a `datetime.timedelta` instance, so it does have a `seconds` attribute. – Lukas Graf Aug 15 '13 at 15:06
  • `AttributeError: 'datetime.timedelta' object has no attribute 'second'` Seconds works just fine. I get no error. The problem is that maybe `starttime` is getting overridden or reset somehow? – mga Aug 15 '13 at 15:07