JRuby vs CRuby #2

Someone said,

try running it a few more times in the script, so Java’s JIT can optimize it.

Yes, that is well known fact. I tried another test according to this proposal.

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

s = last = Time.new
n = ARGV.shift.to_i
count = ARGV.shift.to_i
count.times do
  fib(n)
  t = Time.new
  puts t - last
  last = t
end

Results are showed as following,

% ./bin/jruby fib3.rb 20 5
0.513
0.063
0.113
0.035
0.029
% ruby fib3.rb 20 5
0.016499
0.028091
0.027206
0.017617
0.017504
% ./bin/jruby fib3.rb 30 5
3.149
2.657
2.717
2.702
2.73
% ruby fib3.rb 30 5
2.06232
2.067252
2.086078
2.078028
2.089214

In the case of fib(20), fifth function call is seventeen times faster than first because JIT compiler optimizes program in runtime.
But in the case of fib(30), the difference between first and fifth is just 15%. I think that count of recursive call in fib(30) is too large, so most codes which are executed in the first call are already optimized by JIT compiler.