Ruby Versions and other languages performane comparison

I ran this simple ruby script

max = 5000
z = 0
1.upto(max) do |x|
1.upto(max) do |y|
z = (x+y-z) % 32000
end
end
puts 'Got z = ’ + z.to_s

on different ruby implementations and translations to bash, bc, Java,
perl, python and C. It just does 25 million additions, subtractions and
modulo computations. No real memory handling is involved.

Results on an Intel Pentium 4 CPU 3.00GHz.

2898 seconds – GNU bash, version 3.1.7(1)

112 seconds – bc 1.06

33 seconds – ruby 1.8.4 (2005-12-24) [i386-linux]

20 seconds – ruby 1.9.0 (2006-07-07) [i686-linux]

19 seconds – Python 2.4.2

14 seconds – perl v5.8.8

10 seconds – ruby-yarv / ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08)
[i686-linux]
YARVCore 0.4.0 Rev: 510 (2006-07-06) [opts: ]

5 seconds – ruby-yarv / ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08)
[i686-linux]
YARVCore 0.4.1 Rev: 519 (2006-07-12) [opts: [direct threaded code]
[inline method cache] ]

0.8 seconds – java version “1.5.0_07”
Java™ 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot™ Client VM (build 1.5.0_07-b03, mixed mode, sharing)

0.4 seconds – gcc (GCC) 4.1.0 20060304 (Red Hat 4.1.0-3)

0.2 seconds – same gcc with -O3 compilation flag

So that is nice. ruby-yarv 0.4.1 beats perl 5 by a factor of about 3.
(Note: ruby-yarv 0.4.1 just released yesterday)

Easy to remember:

30 seconds (3 units): ruby 1.8
20 seconds (2 units): ruby 1.9
10 seconds (1 unit) : ruby (yarv 0.4.0)
5 seconds (0.5 unit): ruby (yarv 0.4.1)
less than 1 second : C,Java

Stephan

bash script

#!/bin/bash
let z=0
x=1
while [ $x -lt 5001 ]
do
y=1
while [ $y -lt 5001 ]
do
z=$[ ( $x + $y - $z ) % 32000 ]
y=$[ $y+1 ]
done
x=$[ $x + 1 ]
done

echo “Got $z”

bc script

z = 0
for(x=1; x<=5000; x++)
for (y=1; y<=5000; y++)
z = (x + y - z) % 32000
done
done
print "Result is ", z, “\n”

python script (using max=5001 and range function)

max = 5001
z = 0
for x in range(1,max):
for y in range(1,max):
z = (x+y-z)%32000

print 'Got z = ', z

Perl Code

#!/usr/bin/perl

$max = 5000;
$z = 0;
for ($x = 1 ; $x <= $max; $x++)
{
for ($y=1; $y <= $max; $y++)
{
$z = ($x + $y - $z) % 32000;
}
}
print “Got $z\n”;

Java Code

public class m
{
public static void main(String []argv)
{
int max = 5000;
int z = 0;
int x,y;
for (x = 1; x<=max;x++)
for (y = 1; y<=max;y++)
{
z = (x+y-z) % 32000;
}
System.out.println("Got " + z);
}
}

C Code

#include “stdio.h”
int main(int argc, char **argv)
{
int max = 5000;
int z = 0;
int x,y;
for (x = 1; x<=max;x++)
for (y = 1; y<=max;y++)
{
z = (x+y-z) % 32000;
}
printf(“Got %d\n”, z);
return 0;
}

On Wed, 12 Jul 2006 20:11:53 +0200, Stephan W.
[email protected] wrote:

5 seconds – ruby-yarv / ruby 2.0.0 (Base: Ruby 1.9.0 2006-04-08)
[i686-linux]
YARVCore 0.4.1 Rev: 519 (2006-07-12) [opts: [direct threaded code]
[inline method cache] ]

Here are the results with Ruby2CExtension (HEAD revision, not 0.1.0) on
a
Pentium M 1.5 GHz:

$ ruby -v
ruby 1.8.4 (2005-12-24) [i686-linux]

$ time ruby test.rb
Got z = 20000

real 0m31.163s
user 0m30.829s
sys 0m0.056s

$ rb2cx test.rb

$ time ruby -r test.so -e “”
Got z = 20000

real 0m11.980s
user 0m11.824s
sys 0m0.021s

And it is even faster with while loops:

$ cat test_while.rb
max = 5000
z = x = 0
while (x+=1) <= max
y = 0
while (y+=1) <= max
z = (x+y-z) % 32000
end
end
puts 'Got z = ’ + z.to_s

$ time ruby test_while.rb
Got z = 20000

real 0m35.067s
user 0m34.705s
sys 0m0.059s

$ rb2cx test_while.rb

$ time ruby -r test_while.so -e “”
Got z = 20000

real 0m5.075s
user 0m5.019s
sys 0m0.015s

Dominik

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs