Last active
October 25, 2015 13:10
-
-
Save alexanderjsingleton/1c502d09bb8f2b0fa7e5 to your computer and use it in GitHub Desktop.
A contribution by Flambino to my question at [StackOverflow](http://codereview.stackexchange.com/questions/108617/most-efficient-fizzbuzz-solution-in-ruby#comment199713_108625)
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
require 'benchmark' | |
N = Array(1..10_000_000) | |
def no_op(*a); end | |
def fizzbuzz(array) | |
array.each do |number| | |
divisibleBy3 = (number % 3 == 0) | |
divisibleBy5 = (number % 5 == 0) | |
case | |
when divisibleBy3 && divisibleBy5 | |
no_op "FizzBuzz" | |
when divisibleBy3 | |
no_op "Fizz" | |
when divisibleBy5 | |
no_op "Buzz" | |
else | |
no_op number | |
end | |
end | |
end | |
puts Benchmark.measure{fizzbuzz(N)} | |
puts "fizzbuzz" | |
def super_fizzbuzz(array) | |
array.each do |number| | |
divisibleBy3 = (number % 3 == 0) | |
divisibleBy5 = (number % 5 == 0) | |
if divisibleBy3 && divisibleBy5 | |
no_op "FizzBuzz" | |
elsif divisibleBy3 | |
no_op "Fizz" | |
elsif divisibleBy5 | |
no_op "Buzz" | |
else | |
no_op number | |
end | |
end | |
end | |
puts Benchmark.measure{super_fizzbuzz(N)} | |
puts "super_fizzbuzz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment