Skip to content

Instantly share code, notes, and snippets.

@alexanderjsingleton
Last active December 16, 2015 18:52
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/69301b7b5efe9bbbe77e to your computer and use it in GitHub Desktop.
Save alexanderjsingleton/69301b7b5efe9bbbe77e to your computer and use it in GitHub Desktop.
An experiment supporting the most efficient fizzbuzz solution.
require 'benchmark'
def fizzbuzz(array)
array.map do |number|
divisibleBy3 = (number % 3 == 0)
divisibleBy5 = (number % 5 == 0)
case
when divisibleBy3 && divisibleBy5
puts "FizzBuzz"
when divisibleBy3
puts "Fizz"
when divisibleBy5
puts "Buzz"
else
puts number
end
end
end
puts Benchmark.measure{fizzbuzz(Array(1..100))}
puts "fizzbuzz"
# puts RubyVM::InstructionSequence.disasm(method(:fizzbuzz))
def slower_fizzbuzz(array)
array.map do |number|
if(number % 3 == 0 && number % 5 == 0)
puts "FizzBuzz"
elsif(number % 3 == 0)
puts "Fizz"
elsif (number % 5 == 0)
puts "Buzz"
else
puts number
end
end
end
puts Benchmark.measure{slower_fizzbuzz(Array(1..100))}
puts "slower_fizzbuzz"
# puts RubyVM::InstructionSequence.disasm(method(:slower_fizzbuzz))
# Additional Documentation
# https://en.wikipedia.org/wiki/Assembly_language
# underTheHood => http://ruby-doc.org/core-2.0.0/RubyVM/InstructionSequence.html
# http://ruby-doc.org/stdlib-2.0.0/libdoc/benchmark/rdoc/Benchmark.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment