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(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) 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;
}
--
Posted via http://www.ruby-forum.com/.