Should I use "if" or "try" (as a matter of speed)?

Terry Reedy tjreedy at udel.edu
Sat Jul 9 16:30:50 EDT 2005


"Steve Juranich" <sjuranic at gmail.com> wrote in message 
news:56190b6c05070911213409433e at mail.gmail.com...
> Without fail, when I start talking with some of the "old-timers"
> (people who have written code in ADA or Fortran), I hear the same
> arguments that using "if" is "better" than using "try".  I think that
> the argument goes something like, "When you set up a 'try' block, you
> have to set up a lot of extra machinery than is necessary just
> executing a simple conditional."

I believe 'setting up a try block' is one bytecode (you can confirm this 
with dis.dis).  It is definitely cheaper than making a function call in an 
if condition.  Catching exceptions is the time-expensive part.  For more, 
see my response to 'witte'.

Terry J. Reedy






More information about the Python-list mailing list