Created
January 11, 2014 01:13
-
-
Save AndrewGuard/8365678 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
require 'benchmark' | |
def fib_it(n) | |
fib_nums = [0, 1] | |
iterate = n-1 | |
iterate.times do | |
num = fib_nums[-2] + fib_nums[-1] | |
fib_nums << num | |
end | |
p fib_nums[-1] | |
end | |
# def fib(n) | |
# if n < 2 | |
# n | |
# else | |
# fib(n-1) + fib(n-2) | |
# end | |
# end | |
$cache = [] | |
def fib(n) | |
$cache[n] ||= if n < 2 | |
n | |
else | |
fib(n-1) + fib(n-2) | |
end | |
end | |
puts Benchmark.measure {fib_it(3000)} | |
puts Benchmark.measure {fib(3000)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment