/ update / Article updated on the results of the discussion. The Python code was corrected (about 40% of acceleration), Perl and Ruby code was written (but I’m worried about vague doubts that I did something wrong with ruby), Java code was corrected (on my machine it is more correct to test int, not long besides, int in Java is equivalent to long in C ++).
The question of performance (speed of work) of various languages ​​often pops up in comments, on forums, often unfounded :). There are articles in which the authors give examples where the implementation in one or another language wins.
After reading the next article I wanted to find out for myself “here and now”. At first, I wanted to compare Java and C ++ (I did not believe that in computational tests, Java could overtake and overtake cpp). 10 minutes and simple code in C ++ and Java is ready: a simple loop and mathematical operations. After writing the test I thought and translated them to php and python. Later added code for perl and ruby.
')
So, a few words about the test:
The algorithm is synthetic, a long cycle (two-level) and in it the calculation of a mathematical expression. Thus, the computational performance of the language itself (interpreter or compiled code) is assessed, no bindings to the quality of implementation of certain libraries, no external services, no system-dependent operations (disk, network, graphics).
I will notice in advance:
1) I like Java and I honestly assumed that the results would be better. Updated: long in 64-bit systems works much faster. When working with int in 32-bit Java systems, it speeds up significantly (on my machine, faster than C ++, apparently, JVM optimizes default execution)
2) I guessed that php would be slower than C ++ and Java, but did not think it would be faster than Perl.
3) Assumed that Python would be comparable to PHP, but it was wrong. Apparently, standard PHP delivery optimizes code execution better.
4) I am not at all familiar with Ruby, the code is taken from one of the comments. Moreover, I used
code 1 , since it works faster for me than
code 2 . Perhaps this is also due to the fact that I have a 32bit system.
5) I have a rather respectful attitude to different programming languages, this article is not aimed at inciting holivars in any of the angles. Each language has its own niche and its fans.
Tests were run at least 5 times to avoid accidental surges. They were launched from the console as “nice -n -9”, that is, with the highest priority at the moment in the system.
In order not to force you to read the whole article, I will give brief results immediately.
Chart (updated):

Old version
hereIn the diagram, the Ruby column is partially transparent due to the fact that on my machine the Ruby script was executed indecently long, while in the
commentary it is indicated that the script is executed 4 times faster than the Python script - I am confused.
The column with Python is transparent, because when you turn on the psyco script accelerates more than 10 times. Checked in my car. But this, from my point of view, is a hack that does not reflect the language’s own performance.
A column with PERL, as old-timers can notice, is now on par with Python 2.6. The reason for this was the change of code from a C-like syntax to use range. Additional performance (about 12%) can be obtained by using the “use integer;” directive, but this, in my opinion, is also a hack.
In the form of a table (also updated):
Tongue | Java | Java server | C ++ | C ++, -O2 | Php | Python 2.6 | Python 3.1 | Perl 5.8 | Ruby 1.8 | Ruby 1.9 (?) |
Execution time, sec | 5.3 | 2.8 | 8.5 | 2.6 | 62 | 91 | 145 | | 91 | 207 | ~ 30 |
Productivity,% | 160 | 303 | 100 | 327 | 14 | 9 | 6 | 9 | 4.11 | 28 |
Execution time - at P4-1.8GHz.
Performance - relative to the performance of the basic C ++ code.
Added a column with the launch of Java code with the key "-server". After switching from “long” to “int” (again, int in java is the same as long in c ++ on 32bit-arch), it began to give almost twice the performance gain.
The column with Ruby 1.9 was not tested on my hardware, the result was postponed through comparison with the performance of Python on the same machine.
And, not to be unfounded, the test code.
Java, Test01.java (int in Java is the same as long in C ++):
package ru.dchekmarev.test.performance;
public class Test01 {
public static void main(String[] args) {
// long start = System.currentTimeMillis();
int r = 0;
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
r = (r + (i * j) % 100) % 47;
}
}
System.out.println("answer: " + r);
// , ..
// System.out.println("run time (millis): " + (System.currentTimeMillis() - start));
}
}
C++, Test01.cpp:
#include <iostream>
using namespace std;
int main(void) {
long r = 0;
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
r = (r + (i * j) % 100) % 47;
}
}
cout << "answer: " << r << endl;
}
PHP, Test01.php:
<?php
$r = 0;
for ($i = 0; $i < 10000; $i++) {
for ($j = 0; $j < 10000; $j++) {
$r = ($r + ($i * $j) % 100) % 47;
}
}
echo 'answer: ' . $r . "\n";
?>
Python, Test01.py ( , range() 5% ):
def test():
r = 0
for i in range(0, 10000):
for j in range(0, 10000):
r = (r + (i * j) % 100) % 47
test()
print("answer: ", r)
Perl, Test01.pl (, range 25% c- for):
$r = 0;
# , C-
# for ($i = 0; $i < 10000; $i++) {
# for ($j = 0; $j < 10000; $j++) {
for my $i (0..9999) {
for my $j (0..9999) {
$r = ($r + ($i * $j) % 100) % 47;
}
}
print "answer: $r\n";
Perl, , , .
Ruby, Test01.rb:
r = 0
for i in 0..10_000 do
for j in 0..10_000 do
r = ( r + ( i * j ) % 100) % 47
end
end
puts "answer: #{r}"
erlang.
, : . .
— , .
: , , .
PS: , , , .. , , .