continue and break frustration

G

Glen Wheeler

Hello All.

I've been coding in python for a reasonable amount of time now
(coding in total for approx. 20 years) and am writing a performance
intensive program in python.
The problem I'm having is I want to use break and continue to get
out from more than one level of loop. If I set a variable and then
check it my program will take too much of a performance hit.
Are there any alternatives people can think of?

Thanks,
Glen
 
P

Paul Rubin

Glen Wheeler said:
I've been coding in python for a reasonable amount of time now
(coding in total for approx. 20 years) and am writing a performance
intensive program in python.
The problem I'm having is I want to use break and continue to get
out from more than one level of loop. If I set a variable and then
check it my program will take too much of a performance hit.
Are there any alternatives people can think of?

Raise an exception and catch it. However, if your function is that
sensitive to a little thing like that, maybe you need to write it
differently, for example in Pyrex or as a C extension. You could
also try the Psyco compiler.
 
G

Glen Wheeler

Raise an exception and catch it. However, if your function is that
sensitive to a little thing like that, maybe you need to write it
differently, for example in Pyrex or as a C extension. You could
also try the Psyco compiler.

I've been using the psyco compiler, and it does help (by about 60%).
I'm trying to leave writing the function as a C extension as a last
resort.
I will try with exceptions (get it? :). To the people in the know,
does adding this kind of construct detract from the speed of the code
much? I do know that try+except can be slower than if+else if
exceptions are generated more often than not, but how much slower?

Thanks,
Glen
 
P

Paul Rubin

Glen Wheeler said:
I've been using the psyco compiler, and it does help (by about 60%).
I'm trying to leave writing the function as a C extension as a last
resort.

Pyrex looks really nice for this situation, though I haven't tried it
myself.
I will try with exceptions (get it? :). To the people in the know,
does adding this kind of construct detract from the speed of the code
much? I do know that try+except can be slower than if+else if
exceptions are generated more often than not, but how much slower?

If the exception is usually taken, maybe you can reorganize the code
some way so that it's not.
 
C

Carl Banks

Glen said:
Hello All.

I've been coding in python for a reasonable amount of time now
(coding in total for approx. 20 years) and am writing a performance
intensive program in python.
The problem I'm having is I want to use break and continue to get
out from more than one level of loop. If I set a variable and then
check it my program will take too much of a performance hit.
Are there any alternatives people can think of?

One thing you can do is use an else clause on your inner loop(s) to
continue the surrounding loop. Then, following the else statement,
put in a break. Then, a break in the innermost loop will cascade back
to the top level.

To illustrate:

for i in xrange(100):
for j in xrange(100):
if (i,j) == (45,55):
# Found a match.
# Break out of the inner loop.
break
else:
# No match was encountered in the inner loop.
# Continue the outer loop.
continue
# The inner loop must have found a match.
# So, break out of the outer loop, too.
break

For the above example, this gave me a not-quite-one-percent speed
increase over the version using a variable. YMMV.
 
R

Rene Pijlman

Glen Wheeler:
a performance intensive program in python.
The problem I'm having is I want to use break and continue to get
out from more than one level of loop. If I set a variable and then
check it my program will take too much of a performance hit.
Are there any alternatives people can think of?

Exception handling perhaps. Or old fashioned if-then-else, while etc.

But if the application is performance critical to the extent of one
variable set-and-test, I doubt that Python is the best choice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top