JRuby和Ruby性能简单对比

本文通过实测展示了JRuby相较于Ruby在数学运算、字符串处理和初始化操作上的性能优势,证实了JRuby的执行效率远超Ruby。通过具体代码实现进行测试,得出JRuby在加法、初始化和字符串拼接方面的速度分别为Ruby的10倍、7倍和4.5倍。测试环境为Windows764位系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     网友说Windows上测试不靠谱, 我于是找了台服务器, 配置是真实的,国内很多互联网公司用呢:

 

Intel(R) Xeon(R) CPU E5620 @ 2.40GHz

 

    测试成绩, JRuby 比 Ruby 快 :math 是 3 倍; string 1.6 倍; init  2.4 倍。

 

jruby 1.7.11 (1.9.3p392) 2014-02-24 86339bb on Java HotSpot(TM) 64-Bit Server VM 1.7.0_17-b02 [linux-amd64]:

math 709.0 str 5005.0 init 1480.0

 

ruby 1.8.7 (2011-12-28 patchlevel 357) [x86_64-linux]:

math 2138.454 str 8013.539 init 3694.021

 

    听说Ruby性能有待改进,Java平台上的Ruby又如何呢?于是写了个简单程序测试了一把。结论是, JRuby比Ruby强多了。

    这个测试不算太全面,但也足以说明问题了。PS: 我是在Windows 7 64位上测试的。

    ruby&jruby 版本:

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

 

jruby 1.7.12 (1.9.3p392) 2014-04-15 643e292 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_51-b13  [Windows 7-amd64]

   JRuby执行1000,0000次的成绩 (毫秒,越小越好):

math 482.0
init 484.0
str 1952.0

    Ruby执行1000,0000次的成绩:

math 5090.0
init 3427.0
str 8815.0

    加法, JRuby速度是Ruby的10倍, 初始化7倍,字符串拼接4.5倍。

   附测试代码:

def test_math(times)
    t = 0
    for i in (1..times)
        t+=i
    end
end

def test_str(times)
    for i in (1..times)
        s = ""
        s = s + i.to_s
    end
end

class A
end

def test_init(times)
    for i in (1..times)
        A.new
    end
end

$test_m = Hash.new
$test_m["math"] = method(:test_math)
$test_m["str"] = method(:test_str)
$test_m["init"] = method(:test_init)

$times = ARGV[0].to_i

$test_m.each do |name, m|
        start = Time.now
        m.call($times)
        stop = Time.now
        puts "#{name} #{ (stop - start)*1000 }"
end

转载于:https://www.cnblogs.com/krazysky/p/3672091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值