Update: There are newer benchmark results.
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
|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|
|Python + Psyco||13,305||0,152||13,457||688%||54%||2.6.6||link|
|Ruby||see comment (Ruby seems 35% faster than standard Python)|
|PHP 5.4||roga’s blog results (PHP 5.4 seems 33% faster than PHP 5.3)|
The clear winner among the script languages is… Python. 🙂
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 from http://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 (Jan/11/2013): Added the results for Ruby vs. Python and Nodejs.