Hello,
I was wondering about what exactly makes Perl faster than Ruby, and I made
a small test to examine this a bit. I sort of assumed that it was because
Ruby has more powerful data structures, but it seems that compile-time
optimization might be a significant factor.
The first step of my test was to look at the loop speed of Perl and Ruby.
I ran a loop that did nothing at all about 500,000 times. These are the
average of 3 tries:
ruby 1.023s
perl 1.140s
So Ruby’s loop was faster than Perl’s. I then put ‘val = 2 + 2’ inside
each loop (average of 3 tries):
Total Minus-the-loop
ruby 2.660s 1.637s 100.0%
perl 1.770s 0.630s 38.5%
In the second column I subtract the loop time from the total to get the
time spent in the “val = 2 + 2” step. Here Perl is better than twice as
fast as Ruby.
Now, as many of you know, Perl is not exactly an interpreted language.
Perl first “compiles” the program into some internal bytecode, which is
optimized before being run. For instance, if you have this:
use constant $MYCONST => 3
$val = 2 + $MYCONST
Perl turns it into:
$val = 5
I ran the ruby code again, but this time I replaced ‘val = 2 + 2’ by ‘val
= 4’ to simulate the effect of optimization. Again, the average of 3
tries:
Total Minus-the-loop
ruby 2.660 1.637 100.0%
ruby -optimized 1.793 0.770 47.0%
perl 1.770 0.630 38.5%
Now ther Perl’s operation is only about 18% faster, which in this case
particular case is mitigated by Ruby’s loop being 11% faster. Overall,
the Perl code is only 1% faster (which is less than experimental error).
I realize that the fact that Ruby is a more dynamic language makes
optimization more difficult, but I really think that we should look into
it.
Just a thought.
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137