Skip to content

Instantly share code, notes, and snippets.

@wannabefro
Created January 20, 2014 22:06
Show Gist options
  • Save wannabefro/8530168 to your computer and use it in GitHub Desktop.
Save wannabefro/8530168 to your computer and use it in GitHub Desktop.
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