Last active
December 16, 2015 18:52
-
-
Save alexanderjsingleton/69301b7b5efe9bbbe77e to your computer and use it in GitHub Desktop.
An experiment supporting the most efficient fizzbuzz solution.
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' | |
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