Skip to content

Instantly share code, notes, and snippets.

@headius
Created February 20, 2009 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/67329 to your computer and use it in GitHub Desktop.
Save headius/67329 to your computer and use it in GitHub Desktop.
require 'benchmark'
def fib_ruby(n)
if n < 2
n
else
fib_ruby(n - 2) + fib_ruby(n - 1)
end
end
TIMES = (ARGV[0] || 5).to_i
N = (ARGV[1] || 30).to_i
TIMES.times {
puts Benchmark.measure { fib_ruby(N) }
}
Before (trunk):
$ jruby --fast bench/bench_fib_recursive.rb 20
0.455000 0.000000 0.455000 ( 0.419000)
0.155000 0.000000 0.155000 ( 0.153000)
0.156000 0.000000 0.156000 ( 0.156000)
0.147000 0.000000 0.147000 ( 0.147000)
0.149000 0.000000 0.149000 ( 0.149000)
0.148000 0.000000 0.148000 ( 0.148000)
0.148000 0.000000 0.148000 ( 0.148000)
0.147000 0.000000 0.147000 ( 0.147000)
0.149000 0.000000 0.149000 ( 0.149000)
0.148000 0.000000 0.148000 ( 0.149000)
0.149000 0.000000 0.149000 ( 0.149000)
0.161000 0.000000 0.161000 ( 0.161000)
0.149000 0.000000 0.149000 ( 0.149000)
0.148000 0.000000 0.148000 ( 0.148000)
0.148000 0.000000 0.148000 ( 0.148000)
0.149000 0.000000 0.149000 ( 0.149000)
0.148000 0.000000 0.148000 ( 0.148000)
0.147000 0.000000 0.147000 ( 0.147000)
0.147000 0.000000 0.147000 ( 0.147000)
0.148000 0.000000 0.148000 ( 0.148000)
After manually making +, -, and < direct calls when possible:
$ jruby --fast bench/bench_fib_recursive.rb 20
0.371000 0.000000 0.371000 ( 0.331000)
0.127000 0.000000 0.127000 ( 0.127000)
0.127000 0.000000 0.127000 ( 0.128000)
0.122000 0.000000 0.122000 ( 0.122000)
0.120000 0.000000 0.120000 ( 0.120000)
0.120000 0.000000 0.120000 ( 0.120000)
0.121000 0.000000 0.121000 ( 0.121000)
0.121000 0.000000 0.121000 ( 0.120000)
0.120000 0.000000 0.120000 ( 0.120000)
0.119000 0.000000 0.119000 ( 0.119000)
0.121000 0.000000 0.121000 ( 0.121000)
0.120000 0.000000 0.120000 ( 0.120000)
0.121000 0.000000 0.121000 ( 0.121000)
0.124000 0.000000 0.124000 ( 0.124000)
0.120000 0.000000 0.120000 ( 0.120000)
0.121000 0.000000 0.121000 ( 0.121000)
0.120000 0.000000 0.120000 ( 0.121000)
0.125000 0.000000 0.125000 ( 0.125000)
0.121000 0.000000 0.121000 ( 0.121000)
0.120000 0.000000 0.120000 ( 0.120000)
Theoretical invokedynamic performance (allowing nearly-direct recursive calls to fib as well):
$ jruby --fast bench/bench_fib_recursive.rb 20
0.242000 0.000000 0.242000 ( 0.192000)
0.054000 0.000000 0.054000 ( 0.054000)
0.058000 0.000000 0.058000 ( 0.058000)
0.052000 0.000000 0.052000 ( 0.052000)
0.052000 0.000000 0.052000 ( 0.052000)
0.054000 0.000000 0.054000 ( 0.054000)
0.055000 0.000000 0.055000 ( 0.054000)
0.054000 0.000000 0.054000 ( 0.054000)
0.053000 0.000000 0.053000 ( 0.053000)
0.055000 0.000000 0.055000 ( 0.055000)
0.055000 0.000000 0.055000 ( 0.055000)
0.053000 0.000000 0.053000 ( 0.052000)
0.054000 0.000000 0.054000 ( 0.054000)
0.055000 0.000000 0.055000 ( 0.055000)
0.053000 0.000000 0.053000 ( 0.053000)
0.054000 0.000000 0.054000 ( 0.054000)
0.053000 0.000000 0.053000 ( 0.053000)
0.054000 0.000000 0.054000 ( 0.053000)
0.051000 0.000000 0.051000 ( 0.051000)
0.053000 0.000000 0.053000 ( 0.053000)
Ruby 1.9.1p0:
$ ruby1.9 bench/bench_fib_recursive.rb 20
0.310000 0.000000 0.310000 ( 0.307282)
0.300000 0.000000 0.300000 ( 0.307382)
0.300000 0.000000 0.300000 ( 0.307173)
0.310000 0.010000 0.320000 ( 0.306776)
0.300000 0.000000 0.300000 ( 0.308327)
0.300000 0.000000 0.300000 ( 0.309027)
0.310000 0.000000 0.310000 ( 0.308574)
0.300000 0.000000 0.300000 ( 0.315782)
0.300000 0.010000 0.310000 ( 0.310909)
0.310000 0.000000 0.310000 ( 0.316086)
0.300000 0.000000 0.300000 ( 0.307032)
0.300000 0.000000 0.300000 ( 0.308927)
0.310000 0.000000 0.310000 ( 0.308281)
0.300000 0.010000 0.310000 ( 0.308688)
0.300000 0.000000 0.300000 ( 0.308518)
0.310000 0.000000 0.310000 ( 0.307075)
0.300000 0.000000 0.300000 ( 0.308669)
0.310000 0.000000 0.310000 ( 0.310586)
0.300000 0.010000 0.310000 ( 0.308385)
0.300000 0.000000 0.300000 ( 0.308407)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment