Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexanderjsingleton
Last active October 25, 2015 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderjsingleton/1c502d09bb8f2b0fa7e5 to your computer and use it in GitHub Desktop.
Save alexanderjsingleton/1c502d09bb8f2b0fa7e5 to your computer and use it in GitHub Desktop.
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