Skip to content

Instantly share code, notes, and snippets.

@ryanveroniwooff
Created November 10, 2016 03:59
Show Gist options
  • Save ryanveroniwooff/a4dd841d49edc1e362a9cd75453a63a4 to your computer and use it in GitHub Desktop.
Save ryanveroniwooff/a4dd841d49edc1e362a9cd75453a63a4 to your computer and use it in GitHub Desktop.
#Benchmark Test for Recursive vs Iterative Functions
# of the Fibonacci Sequence Written in Ruby by Ryan Wooff
def recursive_fib(n)
n == 0 || n==1 ? n : recursive_fib(n-1) + recursive_fib(n-2)
end
def iterative_fib(n)
table = [1,1]
n.times do
@fib_table = table[table.length-1] + (table[table.length - 2])
table << @fib_table
end
return table.last
end
require 'benchmark'
num = 35
Benchmark.bm do |x|
x.report("rec:") { recursive_fib(num) }
x.report("itr:") { iterative_fib(num) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment