<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Is PHP even slower than Ruby?</title>
	<atom:link href="http://npzrk8rz.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://npzrk8rz.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Fri, 18 Jan 2008 09:03:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='npzrk8rz.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Is PHP even slower than Ruby?</title>
		<link>http://npzrk8rz.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://npzrk8rz.wordpress.com/osd.xml" title="Is PHP even slower than Ruby?" />
	<atom:link rel='hub' href='http://npzrk8rz.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Ruby vs PHP performance</title>
		<link>http://npzrk8rz.wordpress.com/2008/01/17/ruby-vs-php-performance/</link>
		<comments>http://npzrk8rz.wordpress.com/2008/01/17/ruby-vs-php-performance/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 19:13:43 +0000</pubDate>
		<dc:creator>foobarwidget</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://npzrk8rz.wordpress.com/2008/01/17/ruby-vs-php-performance/</guid>
		<description><![CDATA[This is a mirror for the original blog. If you wish to post a comment, please go to this page instead. It&#8217;s a well-known stereotype that &#8220;Ruby is slow&#8221;. People say it on Slashdot. People say it on OSNews. People say it on my MSN contact list. A few quotes: &#8220;If you care even a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=npzrk8rz.wordpress.com&amp;blog=2547289&amp;post=5&amp;subd=npzrk8rz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="background:#ff9999;margin:1em;padding:0.5em;">This is a mirror for the original blog. If you wish to post a comment, please go to <a href="http://izumi.plan99.net/blog/index.php/2008/01/17/ruby-vs-php-performance/">this page</a> instead.</div>
<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnpzrk8rz.wordpress.com%2F2008%2F01%2F17%2Fruby-vs-php-performance%2F&amp;title=Ruby+vs+PHP%26nbsp%3Bperformance"></a>
<p>It&#8217;s a well-known stereotype that &#8220;Ruby is slow&#8221;. People say it on Slashdot. People say it on OSNews. People say it on my MSN contact list. A few quotes:</p>
<ul>
<li>&#8220;If you care even a little about performance, you don&#8217;t use Ruby.&#8221;</li>
<li>&#8220;You thought Python is slow? Well Ruby is even slower!&#8221;</li>
</ul>
<p>Another stereotype we hear is that &#8220;Ruby on Rails doesn&#8217;t scale&#8221;. Most of the people who say that seem to use PHP, so they imply that PHP does scale.</p>
<p>How much of this is true? If there&#8217;s anything I&#8217;ve learned in the past few years, it&#8217;s that I shouldn&#8217;t always take public opinion seriously. PHP is not really a great language; its interpreter and perhaps even the language specification (if that even exists) has many quirks. My friend Ninh has worked on PHP in the past. He has exposed many of those strange quirks in <a href="http://www.ninh.nl/">his blog</a> (which is, unfortunately, down because of a recent data loss incident).</p>
<p>I can&#8217;t imagine that PHP is faster than Ruby (the language; I&#8217;m not talking about Rails). If Ruby on Rails is slower than PHP, then how much of that is to blame on the language, and how much on the framework? Let&#8217;s put this to the test.</p>
<h2>Goal of and rationale behind this test</h2>
<p>We must first agree on the following premises:<br />
People claim that Ruby is so slow that it&#8217;s impossible to build anything in it that&#8217;s sufficiently fast for &#8220;production environments&#8221;. (1)<br />
Yet at the same time, they accept PHP as being fast enough. (2)</p>
<p>From (1) and (2) follows this question: <b>Is it possible at all to write a web framework in Ruby that&#8217;s faster than PHP?</b></p>
<p>If (1) and (2) are true, i.e. if Ruby web apps are indeed slower than PHP web apps, then the culprit might be:</p>
<ul>
<li>The web framework that the Ruby web app is written in. If this is the case, then it is possible to write a faster web framework that matches PHP&#8217;s performance. (3)</li>
<li>Ruby itself, i.e. the language. If this is the case, then it does not matter what web framework we use or how we write it, it will always be slower than equivalent frameworks PHP. (4)</li>
<li>A combination of both, in which both parts play a significant role. (5)</li>
</ul>
<p><b>The goal of this benchmark is to find out whether (4) is true.</b> (4) is what people have been arguing for years. And that&#8217;s assuming that people know what they&#8217;re talking about; they often don&#8217;t, and confuse the Ruby language with Ruby on Rails the framework, or even with web server Ruby integration modules.</p>
<h2>Benchmarking PHP and Ruby (and Perl, Python, C++)</h2>
<p>I want to benchmark PHP and Ruby&#8217;s <em>code execution speed</em>.</p>
<ul>
<li>I specifically do <b>not</b> want to benchmark code parsing speed. Ruby on Rails apps run in application servers, so all code is only parsed once. PHP has stuff like PHP Accelerator/Zend Accelerator (or whatever those products are called) which cache the parse tree.</li>
<li>I&#8217;m <b>not</b> trying to benchmark HTTP request processing. This is only about code execution speed.
</ul>
<p>So, I decided to implemented the <a href="http://en.wikipedia.org/wiki/Mergesort">mergesort</a> algorithm in PHP and Ruby. The scripts will sort an array of 100 numbers (which where randomly generated and hardcoded into the source code), and perform this sorting 3000 times. The execution times, in seconds, are as follows:<br />
<a href='http://izumi.plan99.net/blog/wp-content/uploads/2008/01/benchmark-with-mergesort.png' title='benchmark-with-mergesort.png'><img src='http://izumi.plan99.net/blog/wp-content/uploads/2008/01/benchmark-with-mergesort.png' alt='benchmark-with-mergesort.png' /></a><br />
As you can see, I&#8217;ve also written a Perl, Python and C++ version just for comparison fun. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The sources code of the test programs can be found on the bottom of this blog post. A few notes:</p>
<ul>
<li>Operating system: Ubuntu Linux 7.10</li>
<li>Hardware: Intel Core 2 Duo T5300 1.73 Ghz, 2 GB RAM</li>
<li>Versions: PHP 5.2.3-1ubuntu6.2, Ruby 1.8.6 (2007-06-07 patchlevel 36), Perl 5.8.8, Python 2.5.1</li>
<li>The test programs not only test raw CPU execution speed, but also memory management speed. As you can see in the merge() functions, it creates a new array, and modify the given arrays. As a result, the mergesort() functions will create a lot of objects that will have to be garbage collected.</li>
<li>Ruby uses a conservative mark-and-sweep garbage collector. Perl and Python both use reference counting. The C++ test program uses Boost&#8217;s shared_ptr, and thus uses reference counting for memory management. I&#8217;m not sure how PHP manages its memory, or whether it does it at all, because all objects are freed after an HTTP request anyway.</li>
<li>The C++ test program is compiled with g++ 4.1, with -O2.</li>
<li>By using Psyco, a JIT optimizer for Python, the Python test program&#8217;s execution time went down to a whopping 0.640 seconds. That&#8217;s even faster than my C++ version (!)</li>
</ul>
<p>But as you can see in the graph, Ruby beats PHP <em>even with a full-blown mark-and-sweep garbage collector</em>. Ruby&#8217;s garbage collector was run several dozens times while running the test program, and Ruby <em>still</em> beats PHP in execution time.</p>
<p>I have no choice but to conclude that all the &#8220;Ruby is slow&#8221; stereotypes are bollocks. PHP <b>the language</b> is slower, yet we do not see anybody complaining about PHP &#8220;scalability&#8221;. The Rails framework could be faster, yes, but at least now we know the language is not as big as a bottleneck as some people might think.<br />
<b>Wait! Do I see a flame thrower there? Read on before you reply!</b></p>
<h2>&#8220;But but but&#8230; you didn&#8217;t benchmark the web environment!&#8221;</h2>
<p>Yes. I did not benchmark the HTTP request handling, and this is intentional. People claim that Ruby is so slow, that it is impossible to write anything in it that&#8217;s acceptably fast. This benchmark proves that, as a language, Ruby is faster than PHP. Therefore, if people can write frameworks in PHP that&#8217;s acceptably fast, then it is also possible to write frameworks in Ruby that&#8217;s acceptably fast.</p>
<p>Note that I do not make any claims about Ruby on Rails&#8217;s speed compared to typical PHP applications.</p>
<h2>&#8220;Why don&#8217;t you use the language&#8217;s builtin sort function? This benchmark is crap!&#8221;</h2>
<p>Implementing sort in the languge instead of using the builtin sort function is intentional. The goal of the benchmark is to test *code execution speed*, not to find out which language can sort faster. This benchmark is a good test because it tests different language primitives, like function calls, comarison operators, arrays, etc. I might as well have implemented A* graph searching, but mergesort is easier.</p>
<h2>Appendix: Source codes</h2>
<ul>
<li><a href="http://pastebin.ca/858829">PHP</a></li>
<li><a href="http://pastebin.ca/858830">Ruby</a></li>
<li><a href="http://pastebin.ca/858831">Perl</a></li>
<li><a href="http://pastebin.ca/858833">Python</a></li>
<li><a href="http://pastebin.ca/858834">C++</a></li>
</ul>
<a class="DiggThisButton DiggMedium" href="http://digg.com/submit?url=http%3A%2F%2Fnpzrk8rz.wordpress.com%2F2008%2F01%2F17%2Fruby-vs-php-performance%2F&amp;title=Ruby+vs+PHP%26nbsp%3Bperformance"></a>
<div style="background:#ff9999;margin:1em;padding:0.5em;">This is a mirror for the original blog. If you wish to post a comment, please go to <a href="http://izumi.plan99.net/blog/index.php/2008/01/17/ruby-vs-php-performance/">this page</a> instead.</div>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/npzrk8rz.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/npzrk8rz.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/npzrk8rz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/npzrk8rz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/npzrk8rz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/npzrk8rz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/npzrk8rz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/npzrk8rz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/npzrk8rz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/npzrk8rz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/npzrk8rz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/npzrk8rz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/npzrk8rz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/npzrk8rz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/npzrk8rz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/npzrk8rz.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=npzrk8rz.wordpress.com&amp;blog=2547289&amp;post=5&amp;subd=npzrk8rz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://npzrk8rz.wordpress.com/2008/01/17/ruby-vs-php-performance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/204784d162fece694532d2ef5cdc5ca5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">foobarwidget</media:title>
		</media:content>

		<media:content url="http://izumi.plan99.net/blog/wp-content/uploads/2008/01/benchmark-with-mergesort.png" medium="image">
			<media:title type="html">benchmark-with-mergesort.png</media:title>
		</media:content>
	</item>
	</channel>
</rss>
