[Tutor] finally without try or except
Steven D'Aprano
steve at pearwood.info
Tue Jul 31 03:03:54 CEST 2012
Tino Dai wrote:
> I just remember that there was a performance hit or something for doing
> that.
> Does that sound familiar to anyone?
Yes, and no.
Creating a try block in Python is lightweight and fast. It is about as fast as
the "pass" statement, that is, pretty damn fast and more or less faster than
doing *anything* at all. But technically, yes, there is overhead to try.
(This is in contrast to Java, where just creating a try block is expensive.)
But *catching* the exception is expensive. Not hugely expensive, it isn't like
it takes a second or two, but it does take maybe ten microseconds[1] or so. If
you're doing a LOT of exception-catching, all those microseconds can add up.
But for the use-case you're talking about, who cares if your program takes an
extra ten microseconds, or even a millisecond? You're never going to notice.
[1] Number plucked out of thin air.
--
Steven
More information about the Tutor
mailing list