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:
Language | CPU time | Slower than | Language version | Source code | |||
---|---|---|---|---|---|---|---|
User | System | Total | C++ | previous | |||
C++ (optimized with -O2) | 1,520 | 0,188 | 1,708 | - | - | g++ 4.5.2 | link |
Java (non-std lib) | 2,446 | 0,150 | 2,596 | 52% | 52% | 1.6.0_26 | link |
C++ (not optimized) | 3,208 | 0,184 | 3,392 | 99% | 31% | g++ 4.5.2 | link |
Javascript (SpiderMonkey) | see comment (SpiderMonkey seems as fast as C++ on Windows) | ||||||
Javascript (nodejs) | 4,068 | 0,544 | 4,612 | 170% | 36% | 0.8.8 | link |
Java | 8,521 | 0,192 | 8,713 | 410% | 150% | 1.6.0_26 | link |
Python + Psyco | 13,305 | 0,152 | 13,457 | 688% | 54% | 2.6.6 | link |
Ruby | see comment (Ruby seems 35% faster than standard Python) | ||||||
Python | 27,886 | 0,168 | 28,054 | 1543% | 108% | 2.7.1 | link |
Perl | 41,671 | 0,100 | 41,771 | 2346% | 49% | 5.10.1 | link |
PHP 5.4 | roga’s blog results (PHP 5.4 seems 33% faster than PHP 5.3) | ||||||
PHP 5.3 | 94,622 | 0,364 | 94,986 | 5461% | 127% | 5.3.5 | link |
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:
http://www.famzah.net/download/langs-performance/
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.
source:http://blog.famzah.net/2010/07/01/cpp-vs-python-vs-perl-vs-php-performance-benchmark/