Exception raising, and performance implications.

Steve Holden steve at holdenweb.com
Tue Oct 4 17:25:34 CEST 2005


leo wrote:
>>However, I think the functionality you're asking for is available as
>>inspect.currentframe(), and if the implementation is in "C" it may have a tiny
>>performance advantage over the Python version.
> 
> 
> You're absolutely right, in fact the code snippet from my OP was taken
> directly from inspect.currentframe. We're intending on using this in
> production, and I'm trying to gauge what the implications may be.
> 
> 
>>Python uses exceptions internally, using StopIteration to terminate the
>>iterator in a for: loop.
> 
> 
> Wow, I was unaware of this. If Python internally uses exceptions, maybe
> they aren't as detrimental as I thought.
> 
> That said, I will be judiciously profiling my code and measuring as
> much as possible, I just wanted to throw this question out to the NG in
> case anyone had done this before (and so I can put off learning the
> profiler a little bit longer :) )
> 
> Thanks all for the replies.
> 
Do note, however, that detecting an exception inside the C framework of 
the interpreter carries less overhead than detecting an exception in 
Python itself.

That said, exceptions are probably rather more "lightweight" than you 
might imagine, so benchmarking (the profiler may not be best - have you 
come across "timeit.py"?) is the best way to go.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list