Created
February 17, 2015 11:23
-
-
Save kennethgeerts/57271b2aa80be28c73d0 to your computer and use it in GitHub Desktop.
recursion
This file contains hidden or 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' | |
class Integer | |
def performant_factorial | |
(1..self).reduce(:*) || 1 | |
end | |
def recursive_factorial | |
self == 0 ? 1 : self * (self - 1).recursive_factorial | |
end | |
end | |
N = 8_000 | |
puts 'Performant:' | |
puts Benchmark.measure { | |
N.performant_factorial | |
} | |
puts 'Recursive:' | |
puts Benchmark.measure { | |
N.recursive_factorial | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment