C++ vs. Python vs. Perl vs. PHP performance benchmark


Update: There is a part #2 of the benchmark results. This page has been translated intoSpanish by Maria Ramos.

This all began as a colleague of mine stated that Python was so damn slow for maths. Which really astonished me and made me check it out, as my father told me once that he was very satisfied with Python, as it was very maths oriented.

The benchmarks here do not try to be complete, as they are showing the performance of the languages in one aspect, and mainly: loops, dynamic arrays with numbers, basic math operations.

Out of curiosity, Python was also benchmarked with and without the Psyco Python extension(now obsoleted by PyPy), which people say could greatly speed up the execution of any Python code without any modifications.

Here are the benchmark results:

LanguageCPU timeSlower thanLanguage
C++ (optimized with -O2)1,5200,1881,708--g++ 4.5.2link
Java (non-std lib)2,4460,1502,59652%52%1.6.0_26link
C++ (not optimized)3,2080,1843,39299%31%g++ 4.5.2link
Javascript (SpiderMonkey)see comment (SpiderMonkey seems as fast as C++ on Windows)
Javascript (nodejs)4,0680,5444,612170%36%0.8.8link
Python + Psyco13,3050,15213,457688%54%2.6.6link
Rubysee comment (Ruby seems 35% faster than standard Python)
PHP 5.4roga’s blog results (PHP 5.4 seems 33% faster than PHP 5.3)
PHP 5.394,6220,36494,9865461%127%5.3.5link

The clear winner among the script languages is… Python. :)

NodeJS JavaScript is pretty fast too, but internally it works more like a compiled language. See the comments below.

Please read the discussion about Java which I had with Isaac Gouy. He accused me that I am not comparing what I say am comparing. And also that I did not want to show how slow and how fast the Java example program can be. You deserve the whole story, so please read it if you are interested in Java.

Both PHP and Python are taking advantage of their built-in range() function, because they have one. This speeds up PHP by 5%, and Python by 20%.

The times include the interpretation/parsing phase for each language, but it’s so small that its significance is negligible. The math function is called 10 times, in order to have more reliable results. All scripts are using the very same algorithm to calculate the prime numbers in a given range. The correctness of the implementation is not so important, as we just want to check how fast the languages perform. The original Python algorithm was taken fromhttp://www.daniweb.com/code/snippet216871.html.

The tests were run on an Ubuntu Linux machine.

You can download the source codes, an Excel results sheet, and the benchmark batch script at:

Update (Jul/24/2010): Added the C++ optimized values.
Update (Aug/02/2010): Added a link to the benchmarks, part #2.
Update (Mar/31/2011): Using range() in PHP improves performance with 5%.
Update (Jan/14/2012): Re-organized the results summary table and the page. Added Java.
Update (Apr/02/2012): Added a link to PHP 5.4 vs. PHP 5.3 benchmarks.
Update (May/29/2012): Added the results for Java using a non-standard library.
Update (Jun/25/2012): Made the discussion about Java public, as well as added a note that range() is used for PHP and Python.
Update (Aug/31/2012): Updated benchmarks for the latest node.js.
Update (Oct/24/2012): Added the results for SpiderMonkey JavaScript.
Update (Jan/11/2013): Added the results for Ruby vs. Python and Nodejs.


类别:技术文章 | 阅读:300461 | 评论:1 | 标签:performance c python perl php


“C++ vs. Python vs. Perl vs. PHP performance benchmark”共有0条留言







taoCMS发布taoCMS 3.0.2(最后更新21年03月15日),请大家速速升级,欢迎大家试用和提出您宝贵的意见建议。