→ ruby benchmark.rb
Rehearsal -------------------------------------------
Without 0.090000 0.000000 0.090000 ( 0.080811)
With 0.080000 0.000000 0.080000 ( 0.082315)
---------------------------------- total: 0.170000sec
user system total real
Without 0.080000 0.000000 0.080000 ( 0.082895)
With 0.090000 0.000000 0.090000 ( 0.083551)
require 'benchmark'
fibonacci = Hash.new{ |h,k| h[k] = k < 2 ? k : h[k-1] + h[k-2] }
Benchmark.bmbm do |x|
x.report('Without') do
1_000_000.times do
fibonacci[10]
end
end
x.report('With') do
1_000_000.times do
begin
fibonacci[10]
rescue Exception => e
end
end
end
end