for loops longer on a P-IV???

Bengt Richter bokr at oz.net
Thu Aug 1 02:51:01 EDT 2002


On Thu, 01 Aug 2002 00:15:05 -0400, Peter Hansen <peter at engcorp.com> wrote:

>Chris wrote:
>> 
>> This is our wait function:
>> def wait(Seconds):
>>         loop = Seconds * 50
>>         for i in range (0,loop):
>>                 win32api.Sleep(20)
>>                 if win32ui.PumpWaitingMessages(0,-1):
>>                         win32api.PostQuitMessage()
>>                         raise exceptions.SystemExit
>> 
How about just noting the time that the wait should expire (now+Seconds
if you don't have another reference than "now" to compute an end time from),
and looping while "now" is less than that? E.g., (untested)
--
from time import clock
def waitUntil(endTime):
    while clock() < endTime:
        win32api.Sleep(20)
        if win32ui.PumpWaitingMessages(0,-1):
            win32api.PostQuitMessage()
            raise exceptions.SystemExit
--
Then call it with an actual end time. To use it for a delta wait from
the current instant, just call waitUntil(clock()+delta).

But if you want to schedule things accurately, don't do that. Instead,
establish a time origin once, e.g., with t0 = tNext = clock() at the
start of a periodic sequence. Then tNext+=delta; waitUntil(tNext) should give
you control that doesn't drift wrt to the clock reference, though it will jitter.

>> A 10 second wait on our P-IV seems to take 15 seconds [....]
>

>I'm not certain what you are actually expecting, but the code you
>have here is effectively the following for a "10 second" delay:
>
> do this 500 times:
>    wait at least 20 milliseconds
>    do some other stuff that takes an undefined amount of time
>
>It looks like you think this should take 500*20 milliseconds
>to complete, but there are at least two sources of additional
>delay -- the "at least" part, and the "other stuff" part.  
>You are actually accumulating error and the finer the period of 
>the sleep, the more inaccurate the overall time will be.
>
>If you're looking for accuracy in timing, this is not the way
>to go about it.
>
I think waitUntil should improve it.

Regards,
Bengt Richter



More information about the Python-list mailing list