[IronPython] Exception Performance

Dave Fugate dfugate at microsoft.com
Tue Aug 3 10:02:45 PDT 2010

Yup, I think you'll find the situation in IronPython 2.7A1 to be a bit better in this respect:
	D:\rft\vsl\dlr\bin>type blah.py
	from System.Diagnostics import Stopwatch
	t = Stopwatch()
	    x = 1/0
	except Exception, e:

	print t.Elapsed.TotalMilliseconds

	D:\rft\vsl\dlr\bin>"C:\Program Files (x86)\IronPython 2.6\ipy.exe" blah.py

	D:\rft\vsl\dlr\bin>"C:\Program Files (x86)\IronPython 2.6\ipy.exe" blah.py

	D:\rft\vsl\dlr\bin>"C:\Program Files (x86)\IronPython 2.6\ipy.exe" blah.py

	D:\rft\vsl\dlr\bin>"C:\Program Files (x86)\IronPython 2.7\ipy.exe" blah.py

	D:\rft\vsl\dlr\bin>"C:\Program Files (x86)\IronPython 2.7\ipy.exe" blah.py

	D:\rft\vsl\dlr\bin>"C:\Program Files (x86)\IronPython 2.7\ipy.exe" blah.py


-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Jeff Hardy
Sent: Tuesday, August 03, 2010 8:41 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Exception Performance

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 System.Double.TryParse (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
Users mailing list
Users at lists.ironpython.com

More information about the Users mailing list