Anomaly in time.clock()

Godzilla godzillaismad at gmail.com
Wed Mar 19 08:17:25 EDT 2008


Hi John,

I am using time.clock to calculate the elapsed time. Below is an
example of what I was trying to do:


import time
import thread

class elapseTime:
    def __init__(self, name=''):
      self.name = name
      self.timeStamp = None
      self.checkTimeFlag = False
      thread.start_new_thread(self.elapsedTime, ())

    def setTime(self, state):
      if state == 1:
        self.checkTimeFlag = True
        self.timeStamp = time.clock()
      else:
        self.checkTimeFlag = False

    def elapsedTime(self):
      while True:
        curTime = time.clock()
        if self.checkTimeFlag:
          if (curTime - self.timeStamp) > 1.0:
            print "Elapsed time greater than 1 second. Actual Elapsed
Time", round(curTime-self.timeStamp, 3)
            self.checkTimeFlag = False
        prevTime = curTime
        time.sleep(0.05)

obj = elapseTime()

while True:
  obj.setTime(1)
  time.sleep(10)


But the time.clock() sometimes return a value of between -3.5 to -4.5
seconds backward. Note that not all computers are behaving the same. I
have not experience the same problem with the computer at home.



More information about the Python-list mailing list