control structures (was "Re: Sins")

Darrell darrell at dorb.com
Thu Jan 6 12:06:22 EST 2000


From: "Skip Montanaro"
>     Emile> Doesn't this do that now? or is there some objection to this
usage?
>
>     [ ... snip try/except used to implement state machine ... ]
>
> I find nothing wrong with it.  Some people object to using try/except for
> anything but handling errors however.

Don't know if this if fair. But exceptions are slow when raised.
import time

def exception(cnt):
    for x in range(cnt):
        try:
            raise 0
        except:
            pass

def loop(cnt):
    for x in range(cnt):
        if x > cnt/2:
            pass
        else:
            pass

size=100000
t1=time.time()
exception(size)
print 'Time:',time.time()-t1

t1=time.time()
loop(size)
print 'Time:',time.time()-t1

######## Output
# Time: 3.59399998188
# Time: 0.25







More information about the Python-list mailing list