Created
May 14, 2011 16:58
-
-
Save denisdefreyne/972380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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