How to let a loop run for a while before checking for break condition?

Claudio Grondi claudio.grondi at freenet.de
Sun Aug 27 08:15:16 EDT 2006


Diez B. Roggisch wrote:
> Claudio Grondi schrieb:
> 
>>
>> Sometimes it is known in advance, that the time spent in a loop will 
>> be in order of minutes or even hours, so it makes sense to optimize 
>> each element in the loop to make it run faster.
>> One of instructions which can sure be optimized away is the check for 
>> the break condition, at least within the time where it is known that 
>> the loop will not reach it.
>>
>> Any idea how to write such a loop?
>>
>> e.g.
>>
>> counter = 2*64
>>
>> while counter(BUT DON'T CHECK IT THE FIRST ONE HOUR LONG):
> 
> 
> now = time.time()
> while time.time() - now < 3600.0 or some_other_condition:
>    ...
> 
> 
> The short circuiting of the or will prevent the execution of 
> some_other_condition.
> 
>>   ... do something ... # and decrease the counter
>>
>> Thanks for any hint, but in particular if related to timers on the 
>> Windows 2000/XP system I am mainly working with.
>>
>> What do you think about this idea? Does it make sense?
> 
> What idea?
This one you haven't probably got from what I have written.
I thought, that the introductory text gives enough context to be able to 
see what I mean, but I was apparently wrong.

The idea is to speed up a loop by using a timer interrupt interfering 
with the loop, so that only after the timer interrupt would occur, the 
loop will start to check its break condition in each iteration.
No checking of any kind in the loop should happen up to that time to 
minimize the number of operations in each iteration within the loop 
itself (i.e. the loop more or less won't know, that there is a timer on 
its way to change the loops behavior at a later time).

I hope this above helps to understand what I would like to achieve.

Claudio Grondi



More information about the Python-list mailing list