最新

val it : α → α = fun

<<  2007/06  >>

2007/06/11 フィボナッチ数ベンチマーク

JRuby と cRuby、それに YARV(ruby-1.9)の比較についてちょろっと書いてから RubyKaigi のページを見たら、速報ログのJRubyの講演にこんなことが書いてあった。

ベンチマーク

フィボナッチ数列の計算では、 JRubyは起動に少し時間がかかるが、起動後はruby-1.8.5より少し速い。

うーむ?

ちなみにわたしのテストコードはこんな感じ。

def fib(n)
  if n == 0 then
    0
  elsif n == 1 then
    1
  else
    fib(n-1)+fib(n-2)
  end
end

puts RUBY_VERSION
s = Time.new
p fib(30)
p Time.new - s

実行結果はこんな感じ。

% time ./bin/jruby ./test.rb
1.8.5
832040
4.488
./bin/jruby ./test.rb  5.22s user 0.12s system 99% cpu 5.383 total
% time ruby ./test.rb
1.8.6
832040
2.029528
ruby ./test.rb  2.02s user 0.01s system 99% cpu 2.040 total
% time /usr/local/ruby-1.9/bin/ruby19 ./test.rb
1.9.0
832040
0.469782
/usr/local/ruby-1.9/bin/ruby19 ./test.rb  0.47s user 0.01s system 99% cpu 0.486 total

環境は Mac OSX 10.4.9 Intel Core Duo なんだが、ご覧のように JRuby では倍くらい時間がかかっている。

フィボナッチ数列って、こういう計算とはまた別な話なのか、何か根本的にカンチガイしているんでしょうか。