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)


注: 本文尽供个人娱乐,欢迎拍砖







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值