Created
November 10, 2016 03:59
-
-
Save ryanveroniwooff/a4dd841d49edc1e362a9cd75453a63a4 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
#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