最近在看jruby的一些书,讲到jruby是用纯java写的一个ruby解释器,于是想测试一下它的性能到底如何
顺便比较一下几种主流的脚本语言的性能
测试算法如下:
ruby:
#!/usr/bin/ruby -w
x=10000
fact=1
i=1
while x >= i
fact*=i
i+=1
end
puts fact
python:
#!/usr/bin/env python
import sys
x=10000
fact=1
i=1
while (x >= i):
fact*=i
i+=1
print fact
perl:
#!/usr/bin/perl -w
use strict;
my $x = 10000;
my $fact = 1;
my $i = 1;
while ( $x >= $i ) {
$fact *= $i;
$i++;
}
print "$fact \n";
是用ruby的benchmark库
#!/usr/bin/ruby -w
#
require 'benchmark'
Benchmark.bmbm do |b|
b.report("ruby") { system("ruby test.rb >/dev/null")}
b.report("python") { system("python test.py >/dev/null")
b.report("perl") { system("perl test.pl >/dev/null")}
end
结果如下:
使用的ruby解释器:
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
ruby 0.000000 0.000000 0.560000 ( 0.546213)
python 0.000000 0.000000 2.010000 ( 2.004604)
perl 0.000000 0.000000 0.020000 ( 0.007585)
--------------------------------- total: 2.590000sec
user system total real
ruby 0.000000 0.000000 0.510000 ( 0.493759)
python 0.000000 0.000000 0.840000 ( 0.823598)
perl 0.000000 0.000000 0.020000 ( 0.009535)
使用ruby解释器:
jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2011-10-27 6586) (OpenJDK 64-Bit Server VM 1.6.0_24) [amd64-java]
Rehearsal ------------------------------------------
ruby 0.000000 0.000000 1.840000 ( 1.508107)
python 0.000000 0.000000 0.790000 ( 0.778767)
perl 0.000000 0.000000 0.020000 ( 0.009619)
--------------------------------- total: 2.650000sec
user system total real
ruby 0.000000 0.000000 1.730000 ( 1.422961)
python 0.000000 0.000000 0.950000 ( 0.934636)
perl 0.000000 0.000000 0.020000 ( 0.008203)
注: 本文尽供个人娱乐,欢迎拍砖