📜 ⬆️ ⬇️

Ruby && Python && Perl && PHP && Ruby1.9

In the comments to my article, there were requests to test the performance of the example given there in other languages. What I was trying to do.
As can be seen from the title, practically all of today's dynamic languages, as well as a new version of Ruby, participated in the test.
Let's take a look at the results.

A few words about the test. For testing the number factorization function was used. The most primitive algorithm is the enumeration of dividers. In each language a class was created (except Perl'a) with one method.
Then it was called 1000 times in a loop, with an argument of 999999.
The system on which the test was made is Debian Lenny. All interpreters are installed by packages from the official repository. The processor - Pentium D 2.8.
The test also involved the Ruby interpreter from the experimental branch 1.9 (also delivered from the repository).
Another remark, maybe I implemented some scripts suboptimally, given the possibilities of languages. I tried to implement the algorithm identically in all languages. If there are comments and additions - please comment.
The following script was written for the test:
     #! / bin / bash
     uname -a # system information
     cat / proc / cpuinfo |  grep "model name" # processor info
     # tests themselves, with a preliminary version of the interpreter
     ruby -v
     time ruby ​​factor.rb
     ruby1.9 -v
     time ruby1.9 factor.rb
     php -v
     time php factor.php
     python -V
     time python factor.py
     perl -v
     time perl factor.pl

Now let's look at the result of executing this script:
     Linux debian 2.6.26-1-686 # 1 SMP Mon Dec 15 18:15:07 UTC 2008 i686 GNU / Linux
     model name: Intel (R) Pentium (R) D CPU 2.80GHz
     model name: Intel (R) Pentium (R) D CPU 2.80GHz
     ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

     real 0m1.099s
     user 0m1.076s
     sys 0m0.000s
     ruby 1.9.0 (2008-06-20 revision 17482) [i486-linux]

     real 0m0.287s
     user 0m0.252s
     sys 0m0.008s
     PHP 5.2.6-0.1 ~ lenny1 with Suhosin-Patch 0.9.6.2 (cli) (built: Nov 29 2008 21:35:12) 
     Copyright (c) 1997-2008 The PHP Group
     Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

     real 0m0.348s
     user 0m0.324s
     sys 0m0.008s
     Python 2.5.2

     real 0m0.537s
     user 0m0.536s
     sys 0m0.000s

     This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

     Copyright 1987-2007, Larry Wall

     Perl may be copied under the terms of either the Artistic License or the
     The GNU General Public License, which can be found in the Perl 5 source kit.

     Complete documentation for Perl, including FAQ lists, should be found on
     this system using "man perl" or "perldoc perl".  If you have access to the
     Internet, point your browser at http://www.perl.org/, the Perl Home Page.


     real 0m0.569s
     user 0m0.552s
     sys 0m0.004s


Summarize. Python is roughly comparable in speed with Perl. Ruby 1.8 naturally loses. PHP is almost the fastest.
And here such a surprise, the Ruby 1.9 experimental branch is the fastest. I did not accidentally include in testing Ruby 1.9. When developing it, emphasis was placed on speed. But, honestly speaking, I am very surprised, I did not expect that it would work so well.
So Ruby fans, we have where to go. Looking forward to Ruby 2.0! :)

Scripts used in the test:
factor.rb , factor.py , factor.pl , factor.php
')
Upd: The comments on the objectivity of the results flared up in the comments. Yes, I do not argue, the test is synthetic, besides a comprehensive analysis of the performance of the target was not.
The results can be considered the following:
1. The speed of execution in these languages ​​is commensurate (at least there are no very significant jumps in this task).
2. Ruby (this benchmark based on an article about Ruby) is successfully developing in terms of performance.
Upd2: Habrauzer deerua for clarity, made a diagram that displays the test results:
benchmark
Upd3: Habrayuzer AlDev did not stand aside, and made another version of the diagram:
bencmark

Source: https://habr.com/ru/post/48952/


All Articles