Created
January 20, 2014 22:06
-
-
Save wannabefro/8530168 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' | |
@cache = Hash.new | |
def fibonacci_cache(n) | |
if t = @cache[n] | |
return t | |
end | |
if n <= 1 | |
return 1 | |
else | |
result = fibonacci_cache(n-1) + fibonacci_cache(n-2) | |
end | |
@cache[n] = result | |
end | |
def fibonacci(n) | |
if n <= 1 | |
return 1 | |
else | |
fibonacci(n-1) + fibonacci(n-2) | |
end | |
end | |
puts Benchmark.measure{fibonacci(40)} | |
puts Benchmark.measure{fibonacci_cache(40)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment