<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sufficiently Small &#187; IronPython</title>
	<atom:link href="http://www.smallshire.org.uk/sufficientlysmall/category/computing/software/python/ironpython/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.smallshire.org.uk/sufficientlysmall</link>
	<description>sin(x) = x</description>
	<lastBuildDate>Sun, 01 Jan 2012 23:29:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Specification of rich comparison protocol use by the Python standard library</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2011/03/12/specification-of-rich-comparison-protocol-use-by-the-python-standard-library/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2011/03/12/specification-of-rich-comparison-protocol-use-by-the-python-standard-library/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 19:27:35 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=508</guid>
		<description><![CDATA[I&#8217;m in the process of writing some code which ideally would work unchanged on CPython 2.x, CPython 3.x IronPython 2.x and Jython 2.x. Given the nature of the code I&#8217;m writing, that seems eminently possible with very few workarounds. One of the changes between Python 2 and Python 3 was the removal of the cmp() [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2011%2F03%2F12%2Fspecification-of-rich-comparison-protocol-use-by-the-python-standard-library%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2011%2F03%2F12%2Fspecification-of-rich-comparison-protocol-use-by-the-python-standard-library%2F&amp;source=robsmallshire&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I&#8217;m in the process of writing some code which ideally would work unchanged on CPython 2.x, CPython 3.x IronPython 2.x and Jython 2.x.  Given the nature of the code I&#8217;m writing, that seems eminently possible with very few workarounds.</p>
<p>One of the changes between Python 2 and Python 3 was the removal of the <code>cmp()</code> function and its corresponding <code>__cmp__()</code> special method.  Python 3 requires the use of the rich comparison operators <code>__lt__()</code>, <code>__gt__()</code>, <code>__ge__()</code>, <code>__le__()</code>, <code>__eq__()</code> and <code>__ne__()</code>.  This change is <a href="http://docs.python.org/release/3.0.1/whatsnew/3.0.html#ordering-comparisons">well publicised</a> and is fine so far as it goes; it&#8217;s usually no problem to modify the code accordingly.  However, the same document goes on to say,</p>
<blockquote><p>Use <code>__lt__()</code> for sorting, <code>__eq__()</code> with <code>__hash__()</code>, and other rich comparisons as needed.</p></blockquote>
<p>This is clear guidance, if not a specification, that sort routines will use <code>__lt__()</code> for sorting.  We don&#8217;t have to look much further for a statement closer to a specification though. In the <a href="http://docs.python.org/howto/sorting.html">Python Sorting HOWTO</a> we can find the following clear declaration:</p>
<blockquote><p>The sort routines are guaranteed to use <code>__lt__()</code> when making comparisons between two objects. So, it is easy to add a standard sort order to a class by defining an <code>__lt__()</code> method.</p></blockquote>
<p>Unfortunately, this fact is not mentioned in the documentation for <code>list.sort()</code> or the <code>sorted()</code> built-in, so it&#8217;s difficult to find. Even more unfortunately for me, the documentation for the standard library <a href="http://docs.python.org/library/heapq.html"><code>heapq</code></a> module does not specify at all what protocols it requires of the objects it will be ordering.</p>
<p>Now all of this became an issue for me today when I implemented an important optimisation which required that I implement an incremental (<em>i.e.</em> lazy) partial sort rather than a complete eager sort of a sequence.  Partial sorts can be efficiently implemented using heaps &#8211; hence my encounter with the <code>heapq</code> module.  My migration from <code>sort()</code> to <code>heapq</code> worked fine on CPython 2.x and CPython 3.x but when I ran my tests with IronPython I got failures for some sorting results.</p>
<p>Soon I determined that the problem was that, as per the documentation for sorting, I&#8217;d implemented <em>only</em> <code>__lt__()</code> and <code>__eq__()</code> rather than the full set of six rich comparison operators.  This worked fine with my original code and with my <code>heapq</code> based sort in CPython because those sort implementation do indeed only call <code>__lt__()</code> and <code>__eq__()</code>.  However, the IronPython implementation of <code>heapq</code> also calls <code>__gt__()</code> and so failed.  Note that IronPython hasn&#8217;t done anything <em>wrong</em> here &#8211; it&#8217;s following the letter of the canonical CPython documentation, if not the spirit. I&#8217;m not sure it&#8217;s fair to consider this a bug in IronPython &#8211; rather it exposes a small but important weakness in the CPython documentation.</p>
<p>There are perhaps two morals to this story: The first is to always implement all or nothing when in comes to the rich comparison operators just like your mother taught you.  The second is that we&#8217;d avoid these sort of difficulties if the Python documentation was a little more explicit about what protocols are expected of objects by its algorithms and containers.</p>
<p>The existence of multiple Python implementations has been extremely effective at defining what it means to be Python as opposed to CPython. Many inconsistencies such as the one noted above have been flushed out over the years. For this reason alone I&#8217;m hesitant to suggest that the folks porting the standard library from CPython to IronPython or Jython should actually read the CPython implementations to ensure compatibility.  I think it&#8217;s much preferable that such things are reimplemented from the documentation and test suite with consequential improvements for both.</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2011/03/12/specification-of-rich-comparison-protocol-use-by-the-python-standard-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OWL BASIC produces its first executable</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/08/04/owl-basic-produces-its-first-executable/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2009/08/04/owl-basic-produces-its-first-executable/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 21:45:26 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[OWL BASIC]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=394</guid>
		<description><![CDATA[After a long haul, and diversions into other more important projects &#8212; including starting a family &#8212; OWL BASIC today produced its first executable. Its not much. In fact its hardly anything. Just 2048 bytes of Windows PE executable containing the global variable declarations from Acornsoft&#8217;s 1982 Sphinx Adventure. Each file of BASIC source code [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F08%2F04%2Fowl-basic-produces-its-first-executable%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F08%2F04%2Fowl-basic-produces-its-first-executable%2F&amp;source=robsmallshire&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>After a long haul, and diversions into other more important projects &#8212; including starting a family &#8212; <a href="http://www.smallshire.org.uk/sufficientlysmall/2007/06/10/writing-a-bbc-basic-compiler-for-the-clr/">OWL BASIC</a> today produced its first executable.  Its not much. In fact its hardly anything. Just 2048 bytes of Windows PE executable containing the global variable declarations from Acornsoft&#8217;s 1982 Sphinx Adventure. Each file of BASIC source code will be converted to a single .NET static class, with the global variables as private static fields.</p>
<div id="attachment_395" class="wp-caption aligncenter" style="width: 538px"><img src="http://www.smallshire.org.uk/sufficientlysmall/wp-content/uploads/sphinx_reflector.png" alt="The first executable produced from OWL BASIC." title="sphinx_reflector" width="528" height="571" class="size-full wp-image-395" /><p class="wp-caption-text">The first executable produced from OWL BASIC.</p></div>
<p>Above you can see the executable loaded up into <a href="http://www.red-gate.com/products/reflector/">.NET Reflector</a>, which can be used to introspect the executable, and in this case attempt to disassemble it into C#.  Now we see what makes .NET such a great platform for compiler construction; below is the <a href="http://www.codeplex.com/IronPython">IronPython</a> source code for the embryonic assembly generation function. It clocks in at fewer than ten lines of code to create an assembly, create a module, create a class, add one private static field to it for each global variable, and save the result as an <code>.exe</code>.</p>
<pre class="brush: python; title: ; notranslate">
def generateAssembly(name, global_symbols):
    domain = Thread.GetDomain()
    assembly_name = AssemblyName(name)
    assembly_builder = domain.DefineDynamicAssembly(assembly_name, AssemblyBuilderAccess.RunAndSave)
    module_builder = assembly_builder.DefineDynamicModule(name + &quot;.exe&quot;)
    type_builder = module_builder.DefineType(name, TypeAttributes.Class | TypeAttributes.Public, object().GetType())

    # Add global variables to the class
    for symbol in global_symbols.symbols.values():
        field_builder = type_builder.DefineField(symbol.name, ctsType(symbol),
                                                 FieldAttributes.Private | FieldAttributes.Static)

    result = type_builder.CreateType()
    assembly_builder.Save(name + &quot;.exe&quot;)
</pre>
<p>where <code>global_symbols</code> is the global symbol table constructed during traversal of the Abstract Syntax Tree and the Control Flow Graph and the <code>ctsType</code> function maps OWL BASIC types to their equivalent <a href="http://en.wikipedia.org/wiki/Common_Type_System">Common Type System</a> types for .NET.  Everything else is provided by <a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx">Reflection.Emit</a> and other parts of .NET.</p>
<p>Its interesting that no validation was applied to the variable names supplied to <code>Reflection.Emit</code>.  As you can see, the variable names still include the <a href="http://en.wikipedia.org/wiki/Sigil_(computer_programming)">sigil</a> suffixes for variable typing (<i>e.g.</i> $ for string) and Reflector happily dissassembles these into invalid C# identifiers.  For the final version these names will need to be mangled (<a href="http://en.wikipedia.org/wiki/Hungarian_notation">Hungarian notation</a>?), or merely de-sigiled if no conflicts result, for compatibility with other .NET languages and tools.</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2009/08/04/owl-basic-produces-its-first-executable/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>String compatibility between Python implementations</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/06/18/string-compatibility-between-python-implementations/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2009/06/18/string-compatibility-between-python-implementations/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 14:28:51 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[Jython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=340</guid>
		<description><![CDATA[Jython and IronPython run on platforms where strings are unicode capable by default. Both implementations have chosen to make str essentially an alias for unicode in Python source code. The bytes type, introduced in PEP358 as part of transition to fully unicode Python 3.0, is unambiguously a sequence of single byte values. We can see [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F06%2F18%2Fstring-compatibility-between-python-implementations%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F06%2F18%2Fstring-compatibility-between-python-implementations%2F&amp;source=robsmallshire&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Jython and IronPython run on platforms where strings are unicode capable by default. Both implementations have chosen to make <code>str</code> essentially an alias for <code>unicode</code> in Python source code. The <code>bytes</code> type, introduced in <a href="http://www.python.org/dev/peps/pep-0358/">PEP358</a> as part of transition to fully unicode Python 3.0, is unambiguously a sequence of single byte values. We can see in the table below that Jython and IronPython are caught between what is on the one hand most practical for interopability with existing code and their host platforms, and on the other hand the Right Thing as delivered by Python 3.0.</p>
<table>
<tr>
<th></th>
<th>Jython 2.5</th>
<th>IronPython 2.6</th>
<th>CPython 2.6</th>
<th>CPython 3.0</th>
</tr>
<tr>
<th>str</th>
<td>multibyte</td>
<td>multibyte</td>
<td>byte</td>
<td>multibyte</td>
</tr>
<tr>
<th>unicode</th>
<td>multibyte</td>
<td>multibyte</td>
<td>multibyte</td>
<td>multibyte</td>
</tr>
<tr>
<th>bytes</th>
<td>byte</td>
<td>byte</td>
<td>byte</td>
<td>byte</td>
</tr>
</table>
<p>It seems clear that if you need to write code that is portable between the different Python implementations you should steer clear <code>str</code> and use <code>bytes</code> and <code>unicode</code> to unambigiously express your intent.</p>
<p>Of course, this is impossible since the Python Standard Library is littered with uses of <code>str</code>. For example, in IronPython <code>pickle.dumps()</code> returns <code>str</code> just like Python 2.6 but the <code>str</code> is actually has multibyte storage.  IronPython hides this well, but the abstraction can leak, resulting in much confusion.  Again Python 3.0 does what is right, and <code>pickle.dumps()</code> returns a <code>bytes</code> instance.</p>
<p>These difficulties are most likely to occur when interfacing with native Java or .NET APIs that expect byte arrays, for example when pickling to database blobs. </p>
<p>In Jython an <code>str</code> instance can be converted to a Java byte array as follows.</p>
<pre class="brush: python; title: ; notranslate">
&gt;&gt;&gt; import jarray
&gt;&gt;&gt; a = jarray.array(&quot;This is  string&quot;, 'b')
&gt;&gt;&gt; a
array('b', [84, 104, 105, 115, 32, 105, 115, 32, 32, 115, 116, 114, 105, 110, 103])
</pre>
<p>The equivalent in IronPython, as provided by <a href="http://www.voidspace.org.uk/python/weblog/">Michael Foord</a>,  being,</p>
<pre class="brush: python; title: ; notranslate">
&gt;&gt;&gt; from System import Array, Byte
&gt;&gt;&gt; a = Array[Byte](tuple(Byte(ord(c)) for c in &quot;This is a string&quot;))
&gt;&gt;&gt; a
Array[Byte]((&lt;System.Byte object at 0x000000000000002B [84]&gt;, &lt;System.Byte object at 0x000000000000002C [104]&gt;, &lt;System.Byte object at 0x000000000000002D [105]&gt;, &lt;System.Byte object at 0x000000000000002E [115]&gt;, &lt;System.Byte object at 0x000000000000002F [32]&gt;, &lt;System.Byte object at 0x0000000000000030 [105]&gt;, &lt;System.Byte object at 0x0000000000000031 [115]&gt;, &lt;System.Byte object at 0x0000000000000032 [32]&gt;, &lt;System.Byte object at 0x0000000000000033 [97]&gt;, &lt;System.Byte object at 0x0000000000000034 [32]&gt;, &lt;System.Byte object at 0x0000000000000035 [115]&gt;, &lt;System.Byte object at 0x0000000000000036 [116]&gt;, &lt;System.Byte object at 0x0000000000000037 [114]&gt;, &lt;System.Byte object at 0x0000000000000038 [105]&gt;, &lt;System.Byte object at 0x0000000000000039 [110]&gt;, &lt;System.Byte object at 0x000000000000003A [103]&gt;))
</pre>
<p>Going back we can use identical code in IronPython and Jython.</p>
<pre class="brush: python; title: ; notranslate">
&gt;&gt;&gt; s = ''.join(chr(c) for c in a)
&gt;&gt;&gt; s
'This is a string'
</pre>
<hr/>Copyright &copy; 2012 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2009/06/18/string-compatibility-between-python-implementations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>IronPython hammers CPython when not mutating class attributes</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-hammers-cpython-when-not-mutating-class-attributes/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-hammers-cpython-when-not-mutating-class-attributes/#comments</comments>
		<pubDate>Fri, 22 May 2009 18:52:58 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[Jython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=171</guid>
		<description><![CDATA[Earlier today I posted the second article in what is turning out to be a short series in the investigation into why the performance of IronPython is around 100× slower than CPython, when running the front-end of my OWL BASIC compiler. The most informative comment was from Curt Hagenlocher who works on IronPython in the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F05%2F22%2Fironpython-hammers-cpython-when-not-mutating-class-attributes%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F05%2F22%2Fironpython-hammers-cpython-when-not-mutating-class-attributes%2F&amp;source=robsmallshire&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Earlier today I posted the <a href="http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/">second article</a> in what is turning out to be a short series in the investigation into why the performance of IronPython is around 100× slower than CPython, when running the front-end of my OWL BASIC compiler.</p>
<p>The most informative <a href="http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/#comment-2071">comment</a> was from <a href="http://blogs.msdn.com/curth/">Curt Hagenlocher</a> who works on IronPython in the Visual Studio Managed Languages group at Microsoft.</p>
<p>Curt suggested,</p>
<blockquote><p>
Try storing the counter as a global variable instead of a class-level member of Node — I think you’ll notice a dramatic improvement.
</p></blockquote>
<p>The modified benchmark program looks like this:</p>
<pre class="brush: python; title: ; notranslate">
counter = 0

class Node(object):

    def __init__(self, children):
        global counter
        counter += 1
        self._children = children

def make_tree(depth):
    if depth &gt; 1:
        return Node ([make_tree(depth - 1), make_tree(depth - 1)])
    else:
        return Node([])

def main(argv=None):
    global counter
    if argv is None:
        argv = sys.argv
    depth = int(argv[1]) if len(argv) &gt; 1 else 10

    root = make_tree(depth)
    print counter
    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main())
</pre>
<h2>A dramatic improvement!</h2>
<p>Well, Curt wasn&#8217;t wrong. This made a phenomenal difference with IronPython completing in only 12% of the time taken by CPython &#8211; over 8× <em>faster</em> with a binary tree depth of 20.</p>
<p>Let&#8217;s look in detail at the results.  All results are from a dual quad-core 1.86 GHz Xeon with 4 GB RAM, and as before each benchmark was run five times, and the shortest time of the five taken.</p>
<p>The three test environments are:</p>
<ol>
<li>Python 2.5.2 x86 32-bit</li>
<li>Jython 2.5rc2 on Java Hotspot 1.6 32-bit</li>
<li>IronPython 2.0 on .NET 2.0 x64</li>
</ol>
<div class="wp-caption alignnone" style="width: 610px"><img alt="The relative performance of the three main Python implementations on a benchmark that uses a global counter, rather than mutating a class attribute." src="/sufficientlysmall/wp-content/ipy_performance/tree_x64_inclusive_global.png" title="The relative performance of the three main Python implementations" width="600" height="450" /><p class="wp-caption-text">Figure 1. The relative performance of the three main Python implementations on a benchmark that uses a global counter, rather than mutating a class attribute.</p></div>
<p>Here we can see how IronPython&#8217;s performance has been improved hugely by this simple change. Although startup time dominates for the smaller problem size, now both Jython <em>and</em> IronPython surpass CPython at around half-a-million nodes.</p>
<p>Removing start-up time, which may be irrelevant for long-running processes, gives us the following chart:</p>
<div class="wp-caption alignnone" style="width: 610px"><img alt="The performance of the three main Python implementations excluding start-up time." src="/sufficientlysmall/wp-content/ipy_performance/tree_x64_exclusive_global.png" title="The performance of the three main Python implementations excluding start-up time." width="600" height="450" /><p class="wp-caption-text">Figure 2. The performance of the three main Python implementations excluding start-up time.</p></div>
<p>Again there is a lot of noise in the data below 1000 nodes, but it is clear that Jython scales better than IronPython, which in turn is scaling better than CPython.</p>
<p>Up until now I&#8217;ve been using a log-log scale in the charts because of the wide variation in performance between the different implementations, but now the performance gap is much closer, it&#8217;s difficult to get a sense of just how <i>much</i> faster IronPython is on the modified benchmark.  Let&#8217;s throw in a log-linear plot to help us appreciate what&#8217;s going on:</p>
<div class="wp-caption alignnone" style="width: 610px"><img alt="Figure 3. A linear representation of the same data as in Figure 2, to highlight the performance multiple between IronPython and CPython in the larger tests." src="/sufficientlysmall/wp-content/ipy_performance/tree_x64_linear_global.png" title="A linear representation of the same data as in Figure 2, to highlight the performance multiple between IronPython and CPython in the larger tests." width="600" height="450" /><p class="wp-caption-text">Figure 3. A linear representation of the same data as in Figure 2, to highlight the performance multiple between IronPython and CPython in the larger tests.</p></div>
<p>It&#8217;s perhaps easier to see now that IronPython is doing in 14 seconds what takes CPython 114 seconds to achieve!</p>
<p>Finally, let&#8217;s plot those results as we did before, as multiples of CPython performance:</p>
<div class="wp-caption alignnone" style="width: 610px"><img alt="Figure 4. Execution time of Jython and IronPython as multiples of CPython performance." src="/sufficientlysmall/wp-content/ipy_performance/tree_x64_relative_global.png" title="Figure 4. Execution time of Jython and IronPython as multiples of CPython performance." width="600" height="450" /><p class="wp-caption-text">Figure 4. Execution time of Jython and IronPython as multiples of CPython performance.</p></div>
<p>It is easy to see that in this chart, once we pass half-a-million tree nodes (a tree depth of 19) that both Jython and IronPython are significantly beating CPython.</p>
<h2>Explanation</h2>
<p>Curt Hagenlocher offers the explanation in a <a href="http://www.reddit.com/r/programming/comments/8mfh7/terrible_ironpython_20_and_pretty_good_jython_25/c09r51x">comment</a> in the <a href="http://www.reddit.com/r/programming/comments/8mfh7/terrible_ironpython_20_and_pretty_good_jython_25/">thread </a> on <a href="http://www.reddit.com">Reddit</a>.</p>
<blockquote><p>
In this particular case, IronPython is slow because of the update to Node.counter. Currently, any update to a class will increment the version number for the class, which will have the effect of invalidating any rules compiled for that class. Effectively, the same rules are getting compiled over and over again. Moving the counter to a global should result in performance on par with that of CPython.
</p></blockquote>
<p>which is absolutely correct, except that he&#8217;s underselling the relative gain. IronPython is not only on a par with CPython, it can outperform it by a factor of eight!</p>
<p>With this knowledge in hand, I can now approach optimization of my OWL BASIC compiler, which lies back at the start of this illuminating tale.</p>
<h2>Conclusion</h2>
<ul>
<li>Avoiding mutation of Python class attributes can have significant benefits for IronPython performance.</li>
<li>Both IronPython and Jython scale better than CPython by this benchmark, and have superior performance for large trees of nodes.</li>
</ul>
<hr/>Copyright &copy; 2012 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-hammers-cpython-when-not-mutating-class-attributes/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>IronPython 2.0 and Jython 2.5 performance compared to Python 2.5</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/#comments</comments>
		<pubDate>Fri, 22 May 2009 11:34:28 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[IronPython]]></category>
		<category><![CDATA[Jython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[BBCBASIC]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[dotNET]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=118</guid>
		<description><![CDATA[IronPython 2.0 can be hundreds of times slower than CPython on some microbenchmarks.  Jython 2.5 can scale better than CPython on those same benchmarks.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F05%2F22%2Fironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.smallshire.org.uk%2Fsufficientlysmall%2F2009%2F05%2F22%2Fironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5%2F&amp;source=robsmallshire&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>My <a href="http://www.smallshire.org.uk/sufficientlysmall/2009/05/17/the-performance-of-python-jython-and-ironpython/">previous post</a> covering the performance problems I&#8217;ve been experiencing with IronPython raised some questions about whether the low performance was an effect peculiar to my system, or to my program &#8212; the <a href="http://www.smallshire.org.uk/sufficientlysmall/2007/06/10/writing-a-bbc-basic-compiler-for-the-clr/">OWL BASIC</a> compiler &#8212; where the problem was first noticed. To briefly recap, I&#8217;d determined that IronPython was around 100× slower that CPython on the same program.</p>
<p>Since then, I&#8217;ve had time to reproduce the results with a small and completely unremarkable Python program, and also to run the tests on a different system. I had suspected that in the OWL BASIC compiler, my Python visitor implementation, which is used in applying transformations to the abstract syntax tree, was to blame. I set about condensing a tree visitor down to a small example, but I never got that far.  It is sufficient to simply <i>build</i> a large binary tree to demonstrate the dramatic differences in the performance characteristics of the three main Python implementations.</p>
<h2>The benchmark</h2>
<p>Here is that test program, which just builds a simple binary tree of objects to the requested depth.</p>
<pre class="brush: python; title: ; notranslate">
class Node(object):
    counter = 0

    def __init__(self, children):
        Node.counter += 1
        self._children = children

def make_tree(depth):
    if depth &gt; 1:
        return Node ([make_tree(depth - 1), make_tree(depth - 1)])
    else:
        return Node([])

def main(argv=None):
    if argv is None:
        argv = sys.argv
    depth = int(argv[1]) if len(argv) &gt; 1 else 10

    root = make_tree(depth)
    print Node.counter
    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main())
</pre>
<p>The program builds a binary tree to the depth supplied as the only command line argument, or ten if one is not supplied. It counts the number of nodes as they a built. Remember that the merits or otherwise of this program are not the point! The point is the performance difference between the Python implementations when it is run.</p>
<p>My benchmarking approach has been to run this script five times for each tree depth from a depth of one, upwards to 22, or until my patience was exhausted.  I&#8217;ve taken the minimum time from each run of five. Since there is a non-linear relationship between the depth of the tree and the number of nodes contained therein, logarithmic axes are used in all the charts that follow.</p>
<h2>64 bit Windows Vista x64</h2>
<p>Here are the results for the first test machine &#8211; with dual quad-core 1.86 GHz Xeons with 4 GB RAM running Vista x64, testing IronPython 2.0.0.0 on .NET 2.0, Jython 2.5rc2 on Java Hotspot 1.6.0 and Python 2.5.2.</p>
<div class="wp-caption alignnone" style="width: 610px"><img alt="Create time for a binary tree including Python virtual machine startup on Windows Vista x64 with 1.86 GHz Xeon processors." src="/sufficientlysmall/wp-content/ipy_performance/tree_x64_inclusive.png" title="Binary tree creation on x64" width="600" height="450" /><p class="wp-caption-text">Figure 1. Creation time for a binary tree including Python virtual machine startup on Windows Vista x64 with 1.86 GHz Xeon processors.</p></div>
<p>In Figure 1 we see that above 1000 nodes or so (tree depth of 10) performance for IronPython begin to degrade rapidly. CPython holds out for another two orders of magnitude before the significant costs begin to be felt . Its interesting to see that although Jython is in the middle of the pack, it scales much better than CPython, surpassing it at around half-a-million nodes (tree depth of 19).</p>
<p>In my application &#8212; a compiler &#8212; virtual machine (VM) start-up time is important; however, in many long-running applications this is not the case, so it is interesting to subtract VM start-up time from each series, which we see in Figure 2, below.</p>
<div class="wp-caption alignnone" style="width: 610px"><img alt="By subtracting VM start-up time, we get a picture more interesting for long-running processes." src="/sufficientlysmall/wp-content/ipy_performance/tree_x64_exclusive.png" title="Execution time excluding VM start-up, on Vista x64 with 1.87 GHz Xeon processors" width="600" height="450" /><p class="wp-caption-text">By subtracting VM start-up time, we get a picture more interesting for long-running processes.</p></div>
<p>Below 100 tree nodes, there is a lot of noise in these measurements. Above 100 nodes its easy to see that the blue IronPython curve is at least two chart divisions above the red CPython curve &#8212; that&#8217;s two orders of magnitude or 100× slower, and getting relatively worse as the size of the tree increases.</p>
<h2>32 bit Windows XP x86</h2>
<p>Responses to my earlier article suggested that trying IronPython 2.0.1 with Ngen&#8217;ed binaries on x86 may make a difference.  Well, to cut a long story short, it doesn&#8217;t, but here are the details.   These tests were run on a 900 MHz Pentium M Centrino laptop with 768 MB RAM, and so cannot be directly compared with those above, although its notable that a one year old workstation is only twice as fast as a five year old laptop.  Moore&#8217;s law certainly isn&#8217;t delivering here!</p>
<div class="wp-caption alignnone" style="width: 610px"><img alt="The performance profiles are very similar with IronPython 2.0.1 on x86." src="/sufficientlysmall/wp-content/ipy_performance/tree_x86_exclusive.png" title="Performance for building a binary tree on a 900 MHz Pentium M." width="600" height="450" /><p class="wp-caption-text">The performance profiles are very simular with IronPython 2.0.1 on x86.</p></div>
<p>On x86, IronPython is still 100× slower than CPython, and Jython still scales better.  It seems the essence of this benchmark is not dependent on which hardware or CLR platform it is run.</p>
<p>I&#8217;ll close by re-presenting the data in the x86 benchmarks as multiples of CPython performance, because it dramatically demonstrates the different responses to the scale of the problem size for IronPython and Jython. Again we see Jython catching up with CPython at a tree depth of 19, just we saw on x64. and IronPython delivering 6000× worse than CPython at a tree depth depth of 15. A tree of this size with thirty-thousand nodes is very similar in scale to the AST tree sizes found in the OWL BASIC during compilation of large programs.</p>
<div class="wp-caption alignnone" style="width: 610px"><img alt="Performance of IronPython and Jython as multiples of CPython performance." src="/sufficientlysmall/wp-content/ipy_performance/tree_x86_relative.png" title="Performance of IronPython and Jython as multiples of CPython performance." width="600" height="450" /><p class="wp-caption-text">Performance of IronPython and Jython as multiples of CPython performance.</p></div>
<h2>Conclusions</h2>
<ul>
<li>
IronPython can be <strong>very</strong> slow, even on programs in the microbenchmark category, which are doing standard operations such as building trees. Presumably there are still significant optimizations to be made in IronPython to bring its performance closer to that of the other Python implementations.  Hopefully, this example and the measurements can contribute to that improvement.
</li>
<li>
Jython may scale better than Python if your application exercises Python in similar ways to this benchmark.  Speculatively, that <i>could</i> have implications for projects such as <a href="http://www.scons.org/">SCons</a>, which build large in-memory graphs.
</li>
<li>I suppose if nothing else we have demonstrated in passing that Java <i>can</i> be faster than C for some non-trivial programs (like a Python interpreter) running a trivial program, like this benchmark.</li>
</ul>
<hr/>Copyright &copy; 2012 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2009/05/22/ironpython-2-0-and-jython-2-5-performance-compared-to-python-2-5/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

