[IronPython] Exception Performance

Jeff Hardy jdhardy at gmail.com
Tue Aug 3 17:41:25 CEST 2010

Hi Cory,

On Mon, Aug 2, 2010 at 12:41 PM, Cory Brostowicz
<cory.brostowicz at gmail.com> wrote:
> Hello,
> I noticed the performance in one of my python scripts has really bad
> performance, so I started profiling different sections of the script to find
> out  where the issue is.  It turns out, the exception handle seems to add
> the biggest chunk of time.

IronPython 2.6 and earlier rely on the .NET exception mechanism.
.NET's exception handling is extremely slow, whereas CPython has an
extremely fast exception handling system. I don't know the details of
why each is the way it is, though - engineering tradeoffs, I guess.

IronPython 2.7 will have a 'lightweight' exception mechanism that
should be closer to the performance of CPython.

> I've fairly new to the Python language, so if
> there is a better way to do what I'm doing, then please let me know.

Unfortunately, I think that's pretty much idiomatic Python, which
often relies on it's very fast exception handling. In .NET you use the
Try* methods instead, which don't throw exceptions.

If you don't need CPython compatibility, you could use
(http://msdn.microsoft.com/en-us/library/994c0zb1.aspx), but I don't
know how to use out parameters from IronPython off the top of my head.

On an unrelated note, you can write your print statement as:

    print "Total: %s RateGet: %s RateSets: %s Exception Handle: %s
Rate: %s" % (t.Elapsed.TotalMilliseconds, rateGet, rateSet,
e.Elapsed.TotalMilliseconds, Rate)

- Jeff

More information about the Ironpython-users mailing list