Anomaly in time.clock()

Godzilla godzillaismad at gmail.com
Thu Mar 20 07:47:00 EDT 2008


Thanks Ross and John for your help. I apologise for the code I posted
earlier not being the full picture of what I was trying to achieve. I
had instantiated multiple instances of elapseTime class and each of
them gets called approximately the same time. Below is the updated
code:


import time
import thread
import Queue

printq = Queue.Queue(0)

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):
      prevTime = time.clock()
      while True:
        curTime = time.clock()
        timeDiff = (curTime - prevTime)
        if timeDiff < 0.0:
          printq.put_nowait('time.clock() has gone backward!!! Time
Diff '+str(timeDiff))
        if self.checkTimeFlag:
          if (curTime - self.timeStamp) > 1.0:
            printq.put_nowait(self.name+", actual Elapsed
Time"+str(round(curTime-self.timeStamp, 3)))
            self.checkTimeFlag = False
        prevTime = curTime
        time.sleep(0.05)

def printmgr():
    while True:
        print printq.get(True)

# Print thread
thread.start_new_thread(printmgr, ())

objList = []
for i in range(10):
  objList.append(elapseTime("obj"+str(i)))

while True:
  for i in range(len(objList)):
      objList[i].setTime(1)
  time.sleep(10)
  print


When I run the above code long enough in the PC I see the
'time.clock() has gone backward!!!' statement being printed once in a
while. I have been reading a thread about invoking time.clock() at
close proximity caused the 'time warp' problem.

John, the PC experiencing this problem is a single core Intel Celeron
1GHz, XP SP2. The PC at home is a AMD Dual Core XP SP2.



More information about the Python-list mailing list