-1

I am trying to do the following to measure how long a function takes over a loop

a = datetime.datetime.now()
while x<1:
   callFunction()
   b = datetime.datetime.now()
   c = b-a  
   print(str(c))

But I am getting this error

   c = b-a
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'float'

What am I doing wrong?

Snake
  • 14,228
  • 27
  • 117
  • 250
  • Is that your whole code? – heemayl Jul 14 '19 at 20:56
  • 2
    And what is `callFunction` doing to `a`? – paxdiablo Jul 14 '19 at 20:57
  • Nothing it has nothing to do with a. It does some image recognition stuffs – Snake Jul 14 '19 at 20:59
  • Actually your absolutly right.. ohh man how did I not see it, I had a somewhere else in code...I should never work on Sunday – Snake Jul 14 '19 at 21:00
  • 1
    There some good ways out there to do this, I'm not talk about the error you got. Because it your value of `a` change somehow. Try other methods in this question [How do I get time of a Python program's execution?](https://stackoverflow.com/q/1557571/6194097), you'll find some good methods from there as well as in this [Python's time.clock() vs. time.time() accuracy?](https://stackoverflow.com/q/85451/6194097) question too. – Kushan Gunasekera Jul 16 '19 at 07:42

1 Answers1

1

Since a is assigned a datetime value and it later complains about it being a float, there's a very good chance that the callFunction() function is changing it.

I'd be looking into that as a first step.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953