"Bench" thing is ... After several days of inactivity, withdrawal begins, I want to occupy myself with something. Sometimes I was distracted by pet projects, sometimes by reading literature ... Now I will tell you about what happened during the last "standby mode".
For many years I was worried about the performance of the programming language (mainly interested in PHP ). The list below contained some of my beliefs, until recently:
PHP is one of the slowest programming languages
Python is faster than PHP
Ruby is faster than PHP
C / C ++ is much faster than Python and PHP combined
Assembler is an order of magnitude faster than C / C ++
The first step was to decide which test to take as a basis. Let's try to write a program for finding prime numbers (I am aware that there are many algorithms for finding prime numbers, a good article on this topic).
The following code was taken as a basis (primitive enumeration, which does not even stop if it already knows that the number is not prime):
<?php
$primeNumberCount = isset($argv[1]) ? $argv[1] : 100;
$number = 0;
while ($primeNumberCount > 0) {
$number++;
$j = 0;
for ($i = 1; $i <= $number; $i++) {
if ($number % $i === 0) {
$j++;
}
}
if ($j === 2) {
$primeNumberCount--;
}
}
echo 'The latest prime number: ' . $number . PHP_EOL;
And then everything is like a fog: Python, C / C ++, Pascal, Go, etc. All sources can be viewed here . I did all the tests in docker so as not to clog up the computer.
, . Assembler , - . , Assembler/NASM , .
:
, :
PHP Python Ruby
PHP
Python 3 Python 2
/C++ Assembler/NASM 15%
Rust
very surprised by the Node.js / Javascript test result (V8 developers are handsome)
etc
At the moment, I plan to gradually add new tests (when time and mood permits).
The purpose of this article is not to show which programming language is the fastest, but that we can be mistaken in our beliefs, and that we should not believe all the stories in the smoking room (many of my delusions are from there, someone somewhere heard that X is faster than Y).
PPS. Added Ruby 3 , something didn't help much ...
PPPS. Changed the method of calculating the execution time, increased the ordinal number of a prime number (from 5000 to 7000).