Elliott C. Back: In Aere Aedificare

Ruby vs PHP Performance Revisited

Posted in Code, Web 2.0, Scalability, Performance by Elliott Back on January 17th, 2008.

Ignoring any of Hongli Lai’s actual code, I reran the PHP, Ruby, C++, Perl, and Python mergesort benchmarks he gave, and came up with substantially different results. Here are the versions of the programming languages I am using for the test:

  • PHP - PHP 5.1.6 (cli) (built: Sep 18 2007 09:07:28)
  • Ruby - ruby 1.8.5 (2007-09-24 patchlevel 114) [x86_64-linux]
  • Perl - This is perl, v5.8.8 built for x86_64-linux-thread-multi
  • Python - Python 2.4.4 (#1, Oct 23 2006, 13:58:18)
  • C++ - gcc version 4.1.2 20070626 (Red Hat 4.1.2-13)
  • Java - Java(TM) SE Runtime Environment (build 1.6.0_10-ea-b10)

You’ll notice I’m adding Java into the mix for fun. Here’s the results, over 10 runs, on an Intel Dual-core 1.80GHz machines with 2Gb of RAM currently running this website:

mergesort-performance.png

Lang	Average	Min	Max
PHP	8.8325	8.637	9.303
Ruby	7.2896	7.143	7.729
Perl	4.3231	4.262	4.428
Python	3.3465	3.289	3.417
C++	0.5638	0.53	0.609
Java	0.4062	0.262	0.551

There are a couple important conclusions to note here that are significantly different than Hongli Lai’s:

  • PHP is 21% slower than Ruby, not 41% as in his benchmark
  • Python is 29% faster than Perl, not 17% as in his benchmark
  • Java runs this 39% faster than C++, and 2100% faster than PHP

So, PHP is slower than Ruby, but not quite as slow as Hongli Lai would have you believe. Python is the fastest scripting language in this benchmark, while Java is the faster language all around, and is incredibly, incredibly fast. Maybe all of our code should start using java!

* NOTE: I am ignoring the obvious deficiencies of this micro-benchmark and just trying to reduplicate it. What I’ve found is that there are significant discrepancies between Hongli Lai’s run of the tests and my own, probably owing to slightly different versions of the components involved. Also, if I make some trivial optimizations to the loops in the PHP script, I can get it to run faster than everything but C++, in about 2.4s. Then again, just calling sort() is faster by another two orders… but still half as slow as Java’s built-in sort… and two orders slower than perl’s built-in.

This entry was posted on Thursday, January 17th, 2008 at 8:24 pm and is tagged with ruby ruby, intel dual core, gcc version, java note, scripting language, programming languages, runtime environment, java java, optimizations, using java, lai, 4s, php script, cli, php 5, discrepancies, deficiencies, 2gb, python, benchmarks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

12 Responses to 'Ruby vs PHP Performance Revisited'

  1. […] TIOBE Programming Community Index for January 2008によると2007年に世界で一番人気が伸びた言語はPythonだそうです。 ZDNet Japanの記事によると この1年でPythonは2.04%の伸びを見せ、もっとも人気が伸びたプログラミング言語となった。ほかにはVBが1.84%、Javaが1.69%、 C#が1.34%、PHPが1.25%、Delphiが1.00%、JavaScriptが0.36%人気を伸ばしている。 日本ではRuby等の言語に押されて不人気なイメージですが世界では伸びているようです。 ところで、言語の実行速度性能が気になり調べてみました。 動いている環境や実行した関数に依存するため一概には言えませんが、Elliott C. Backのマージソートの実行時間の比較では、PHP>Ruby>Perl>Python>C++>Javaだそうです。赖洪礼的 blogも同じ実験をしており結果は少し異なるものの、順位は同じです。 実行時間はなんとなく、Ruby>PHP>Python=Perl>Java>Cかなぁと思っていました。 Python、速いですね。インデントが面倒で使うのをやめましたが、また使ってみようかという気になります。 via ZDNet Japan via Elliott C. Back via 赖洪礼的 blogC++ java perl php python ruby software スクリプト言語 ニュース 速度比較 Software, ニュース | 7:25 pm 関連エントリ 初代SimCityのソース公開へオープンソースソフトを簡単にインストール出来るサービス / BitNamiMacBook Airに対する市場の反応は厳しい?CES 2008、気になる製品のまとめBug LabsのBUG、2008年末に日本でも購入可能に? […]

  2. wild bill said:

    on January 19th, 2008 at 3:30 am

    i have a little confusion on the numbers presented above.. coz i havent tried comparing those languages on my own… :’(

  3. james said:

    on February 1st, 2008 at 3:58 pm

    Did you replace Hongli’s “while (count($a)>0″ with the correct “while (!empty($a)”?

  4. Elliott Back said:

    on February 2nd, 2008 at 12:46 am

    I don’t remember, but it shouldn’t make a huge difference.

  5. james said:

    on February 4th, 2008 at 8:25 pm

    Using “!empty($a)” is 50% faster than “count($a) >0″, according to my test (almost the same code, Xeon 2.4 GHz, 1GB). I did a 10-sample experiment and at alpha = 0.01 the t value came out to 150.

    If you want to see the experiment: jamessocol.com/projects/count_vs_empty.php

  6. Elliott Back said:

    on February 4th, 2008 at 8:35 pm

    Interesting, I didn’t expect it to make that much difference. Good work, James!

  7. james said:

    on February 4th, 2008 at 9:48 pm

    I gotta use my math degree for something. A 50% speed increase for PHP would reverse the results of the experiment.

  8. vlad said:

    on May 1st, 2008 at 9:23 pm

    Bollocks. We’d like to see the source code for your Java version. I guess this benchmark is just an illusion.

  9. Elliott Back said:

    on May 1st, 2008 at 11:14 pm

    Oh, say something like this, perhaps. Java’s really about the same speed as C++ for most stuff these days.

  10. vlad said:

    on May 1st, 2008 at 11:47 pm

    Eh … Well … I know my comment was a little offensive. I know that you’re a facebook developer and you’re all big php fans so I guess you’re a little subjective. Excuse my english.

  11. Chris said:

    on May 21st, 2008 at 3:15 pm

    Hey

    php with the eaccelerator.net/ is 4 - 5 x quicklier. you need more memory but i think it’s ok. the result from the (example) 32 mb is performance.

    when you published a benchmark, please optional with php eaccelerator.

    in the other situation is the benchmark not fair and correct!

  12. Math Parser Guy said:

    on June 27th, 2008 at 5:42 pm

    C++ gets faster in cases where you can achieve the same end result by writing the code in a C++ specific way which is not available in other languages. For example, do the following:

    Find a sequence of 4 bytes inside a byte array of 256 bytes. Java and C++ solutions has to be coded differently to make C++ faster. Solution is left to the user as exercise.

    Similarly, have a sophisticated algorithm where you need OUT parameters. For java, you would have to work around it. C++ gives that capability naturally.

    Another case: Try to control the floating point processor for math operations in Java (Can you make Java throw Exception on division by zero?). In Visual C++ you can do that too.

Your Thoughts Go Here:

Powered by WP Hashcash