How to let a loop run for a while before checking for break condition?
Hendrik van Rooyen
mail at microcorp.co.za
Tue Aug 29 12:11:36 EDT 2006
"Hendrik van Rooyen" <mail at microcorp.co.za> wrote:
8<-------------------------------------
Here are some more results, three runs without, and three with a comment in the
body of the interesting loop:
(a summary follows the detail)
> python junk.py
0x5000001
Loop 1 Elapsed time is: 31.2168951035
Loop 1 used : 16.39
Time out used was : 28
comment removed from Loop 1
0x5000001
Loop 2 Elapsed time is: 75.8846828938
Loop 2 used : 37.93
> python junk.py
0x5000001
Loop 1 Elapsed time is: 31.3769760132
Loop 1 used : 16.1
Time out used was : 28
comment removed from Loop 1
0x5000001
Loop 2 Elapsed time is: 76.4928090572
Loop 2 used : 38.23
> python junk.py
0x5000001
Loop 1 Elapsed time is: 34.8213500977
Loop 1 used : 18.24
Time out used was : 28
comment removed from Loop 1
0x5000001
Loop 2 Elapsed time is: 82.6038169861
Loop 2 used : 39.13
> python junk.py
0x5000001
Loop 1 Elapsed time is: 36.7637741566
Loop 1 used : 18.81
Time out used was : 28
comment inserted into Loop 1
0x5000001
Loop 2 Elapsed time is: 77.6277718544
Loop 2 used : 38.82
> python junk.py
0x5000001
Loop 1 Elapsed time is: 37.6858890057
Loop 1 used : 19.71
Time out used was : 28
comment inserted into Loop 1
0x5000001
Loop 2 Elapsed time is: 76.0855400562
Loop 2 used : 38.04
> python junk.py
0x5000001
Loop 1 Elapsed time is: 35.0458369255
Loop 1 used : 18.32
Time out used was : 28
comment inserted into Loop 1
0x5000001
Loop 2 Elapsed time is: 75.850135088
Loop 2 used : 37.9
Summary of results:
Without comment:
Average elapsed time calculated by hand: 32.47
Average clock.clock() time : 16.91
With comment:
Average elapsed time calculated by hand: 36.39
Average clock.clock() time : 18.95
Normal loop performance over 6 runs:
Average elapsed time calculated by hand: 77.42
Average clock.clock() time : 38.34
Conclusion 1: The lower level test for the signal is far more efficient than
the Python comparison. - these kind of shenanigans are well worth while if the
loop is known to be a long running one.
Conclusion 2: The setting of the appropriate time out is a PITA... (any ideas?)
Conclusion 3: Don't put comments in the body of long running loops...
Conclusion 4: (from earlier in the thread) - using calls are expensive...
Conclusion 5: I learnt something - Thank you all...
Off topic Question - I see that giving the signal.alarm a float instead of an
integer gives a deprecation warning - does anybody know why this was decided? (I
was trying to do a binary search...)
Here is Claudio's code as I have hacked it:
import signal, time
def func1(timeout):
def callback(signum, frame):
raise EOFError # could use a custom exception instead
signal.signal(signal.SIGALRM, callback)
signal.alarm(timeout)
count = 0
try:
while 1:
# this is a comment in the body of the loop
count += 1
except EOFError:
while True:
count += 1
if count > 0x5000000:
break
print hex(count)
def func2():
count = 0
while True:
count += 1
if count > 0x5000000:
break
print hex(count)
def func3():
return 0
timeout = 28
print
startTime = time.time()
start_use = time.clock()
func1(timeout)
eltime = time.time() - startTime
utime = time.clock() - start_use
print "Loop 1 Elapsed time is:", eltime
print "Loop 1 used :", utime
print "Time out used was :", timeout
print "comment inserted into Loop 1"
print
startTime = time.time()
start_use = time.clock()
func2()
eltime = time.time() - startTime
utime = time.clock() - start_use
print "Loop 2 Elapsed time is:", eltime
print "Loop 2 used :", utime
print
- Hendrik
More information about the Python-list
mailing list