Elliott C. Back: Internet & Technology

Ruby vs PHP Performance Revisited

Posted in Code, Performance, Scalability, Web 2.0 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.

21 Responses to “Ruby vs PHP Performance Revisited”

  1. Boris says:

    There is something wrong here. Its technically impossible that java is faster than gcc c++. PHP performance is much faster with a typical bytecode-cache or php to C compiler.

  2. Tom Cruise says:

    kkkkk, you did a big mistake.
    What ? java is faster than C++ ? what do you want more? Santa Claus really exists ? Easter bunny really exists?

  3. adrey says:

    Ruby forever

  4. Um… Java is faster than C++? It doesn’t make any scenes to me.

  5. S Singh says:

    10 runs? How can you gauge a language performance with just 10 runs?

  6. A count in a while, is the code optimised? And Java is faster than C++ is a news for me. Just for fun, I will share your post with a C++ fanboy who hates Java and will see how he reacts ;-)

  7. Sujai SD says:

    Good Comparison….

    This explains, how slow the PHP is in function calling and arithmetic comparison. But, This result cannot be really considered, as the true application works with PHPs native function calls, These functions are of extensions from C/C++.

    The real PHP applications will be faster than, Ruby because of the native functions usage. Adding to that, the frameworks cannot be as fast as Ruby Frameworks because of its own function calls.

    The data base access in PHP again goes to native library calls, PHP libraries even takes care of json conversion, libxml in the native library.

    Beyond this, PHP module remains loaded in Apache in memory, which never loads everything like it does in the shell.

    Can we have a comparison for a solution in web, where native library use, also an other comparison with PHP Zend / Symphony with RoR?
    That says what is fast? Because it is of no use to say what is fastest, unless knowing how it really performs in the real-time problem.

    PHP’s power lies in the web….
    (Again on web, Try with a Zend Server CE if you want to try ruby with Mongrel/Webrick)

    Your comparison for Java is right because the applications in Java, works 100% on Java.

    This comparison seems incomplete because of few more lagging information, Will it be possible to get a new version of this, with more details?

  8. Luis José says:

    You should try ruby 1.9 and see what happens…

  9. 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.

    • Lorrat says:

      java like c++ is spec’ed to throw exceptions on divisions by zero. You don’t “make” it – it does it all by it self.

  10. Chris says:

    Hey

    php with the http://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!

  11. vlad says:

    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.

  12. Elliott Back says:

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

  13. vlad says:

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

  14. james says:

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

  15. Elliott Back says:

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

  16. james says:

    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: http://jamessocol.com/projects/count_vs_empty.php

  17. Elliott Back says:

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

  18. james says:

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

  19. wild bill says:

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

  20. [...] 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年末に日本でも購入可能に? [...]

Leave a Reply

Powered by WP Hashcash