Created
August 31, 2016 11:34
-
-
Save mcelaney/d20353996877d5603107c6f5ee889c35 to your computer and use it in GitHub Desktop.
Ugly but fast
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
class FizzBuzz | |
def self.find_answers_for(max) | |
self.new.find_answers_for(max) | |
end | |
def initialize | |
@result = [ | |
Proc.new do |x| x; end, | |
Proc.new do |x| x; end, | |
Proc.new do |x| "Fizz"; end, | |
Proc.new do |x| x; end, | |
Proc.new do |x| "Buzz"; end, | |
Proc.new do |x| "Fizz"; end, | |
Proc.new do |x| x; end, | |
Proc.new do |x| x; end, | |
Proc.new do |x| "Fizz"; end, | |
Proc.new do |x| "Buzz"; end, | |
Proc.new do |x| x; end, | |
Proc.new do |x| "Fizz"; end, | |
Proc.new do |x| x; end, | |
Proc.new do |x| x; end, | |
Proc.new do |x| "FizzBuzz"; end, | |
] | |
end | |
def find_answers_for(max) | |
1.upto(max).map { |x| @result[(x - 1) % 15].call(x) } | |
end | |
end | |
puts Benchmark.measure { FizzBuzz.find_answers_for(1000000) } | |
# 0.200000 0.010000 0.210000 ( 0.213481) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment