Skip to content

Instantly share code, notes, and snippets.

@denisdefreyne
Created May 14, 2011 16:58
Show Gist options
  • Save denisdefreyne/972380 to your computer and use it in GitHub Desktop.
Save denisdefreyne/972380 to your computer and use it in GitHub Desktop.
class FibSlow
def run(n)
if n == 0
0
elsif n == 1
1
else
run(n-1) + run(n-2)
end
end
end
class FibFast
extend Nanoc3::Memoization
def run(n)
if n == 0
0
elsif n == 1
1
else
run(n-1) + run(n-2)
end
end
memoize :run
end
def bm
a = Time.now
yield
b = Time.now
b - a
end
slow = bm { puts FibSlow.new.run(35) }
fast = bm { puts FibFast.new.run(35) }
puts "slow = #{slow}"
puts "fast = #{fast}"
# output:
#
# 9227465
# 9227465
# slow = 4.484432
# fast = 0.000808
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment