ruby python perl算階乘性能比較

最近在看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]


Rehearsal ------------------------------------------
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)


注: 本文盡供個人娛樂,歡迎拍磚







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章