简体   繁体   English

Rubinius和JRuby怎么可能这么慢?

[英]How can Rubinius and JRuby possibly be this slow?

I decided to see how long it would take to iterate through an array of hashes. 我决定看看迭代一系列哈希需要多长时间。 Below is the code: 以下是代码:

pairs = [{name: "firstname", value: "string"},{name: "lastname", value: "string"},{name: "country", value: "string"},{name: "city", value: "string"},{name: "state", value: "string"},{name: "company", value: "string"},{name: "year", value: "string"},{name: "political_affiliation", value: "string"},{name: "social_security_number", value: "string"}] * 1000
blank = {}

start = Time.now
pairs.each do |pair|
  blank[pair[:name]] = pair[:value]
end

p Time.now - start

Time is calculated by subtracting the current time after the loop from the current time before the loop. 通过从循环之前的当前时间减去循环之后的当前时间来计算时间。

This was the amount of time the computation took in YARV 2.1.1, according to the math in the code: 根据代码中的数学计算,这是YARV 2.1.1中计算所花费的时间:

0.001962017

Here's how long it took in Rubinius 2.2.6: 这是Rubinius 2.2.6花了多长时间:

0.022598

And jRuby 1.7.12 和jRuby 1.7.12

0.022317

Supposedly Rubinius and jRuby have performance advantages over YARV. 据说Rubinius和jRuby比YARV具有性能优势。 Why do they take almost 12 times the amount of time to perform the same basic operation? 为什么他们花费的时间几乎是执行相同基本操作的12倍? Is this normal or do I have something improperly configured? 这是正常的还是我有不正确的配置?

You are benchmarking too tiny times, which are compromised by enviroment loading. 您的基准测试时间太短,受到环境负载的影响。 In my experience, in order to have reliable benchmarks you have to get timings of at least 10 seconds, in order to mitigate warmup times. 根据我的经验,为了获得可靠的基准测试,您必须获得至少10秒的时间,以减少预热时间。 Around 10 seconds I expect JRuby to be the most performant, followed by Ruby and Rubinius. 大约10秒钟我希望JRuby是最高性能的,其次是Ruby和Rubinius。

Let's see: 让我们来看看:

# so_24049371.rb

require 'benchmark'

# NOTE THIS: YOU SHOULD TWEAK IT IN ORDER TO HAVE BENCHMARKS OF ~ 10 SECONDS
MULTIPLIER = 5_000_000
pairs = [{name: "firstname", value: "string"},{name: "lastname", value: "string"},{name: "country", value: "string"},{name: "city", value: "string"},{name: "state", value: "string"},{name: "company", value: "string"},{name: "year", value: "string"},{name: "political_affiliation", value: "string"},{name: "social_security_number", value: "string"}] \
  * MULTIPLIER
blank = {}

puts Benchmark.measure {
  pairs.each do |pair|
    blank[pair[:name]] = pair[:value]
  end
}

# so_24049371.fish

source (rbenv init -|psub)

for ruby_version in 2.1.2 rbx-2.2.7 jruby-1.7.12
  rbenv shell $ruby_version
  ruby -v
  ruby so_24049371.rb
end

This is the output on my machine (I use fish shell + rbenv, you should write your own script): 这是我机器上的输出(我使用fish shell + rbenv,你应该编写自己的脚本):

> fish so_24049371.fish
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
  8.190000   0.000000   8.190000 (  8.188726)
rubinius 2.2.7 (2.1.0 build 2014-05-20 JI) [x86_64-linux-gnu]
 14.359762   0.003525  14.363287 ( 14.193565)
jruby 1.7.12 (2.0.0p195) 2014-04-15 643e292 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_55-b13 [linux-amd64]
  4.570000   0.000000   4.570000 (  4.367000)

As I supposed, JRuby is the fastest with 4.367000, than Ruby with 8.188726 and last Rubinius with 14.193565. 正如我想象的那样,JRuby是最快的4.367000,而Ruby是8.188726,最后的Rubinius是14.193565。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM