Most efficient FizzBuzz solution in Ruby I would appreciate any feedback concerning my analysis of the most efficient FizzBuzz solution programmed in Ruby. I submit that a Case-statement solution is more efficient than utilizing a ConditionalIf-statement in my recent blog-post within my conclusion provided thusly:


  Consequently, the predictability pattern is the reason why a "Case" statement, or branchIf, is optimal and less expensive than a "Conditional If" statement, as clarified by Igor Ostrovsky's Blog Post: Fast and Slow If-Statements: Branch Prediction in Modern Processors
  
  
    "If the condition is always true or always false, the branch prediction logic in the processor will pick up the pattern. On the other hand, if the pattern is unpredictable, the if-statement will be much more expensive."
  
  
  Back to my optimized FizzBuzz solution- when the "Case" statement processes a number initializing the method, the constraint or case stops calculating when the condition is satisfied, and it will not continue to verify the constraint by branching unless divisible as it were for an IF/ELSIF construction, which saves time, performing faster, as the best solution possible, ultimately proving Danielle Sucher's point:
  
  
    "I'd expect if/elsif to be faster in situations where one of the first few possibilities is a match, and for case to be faster in situations where a match is found only way further down the list (when if/elsif would have to make more jumps along the way on account of all those branchUnlesses)."
  
  
  Furthermore, a real programmer can impress an interviewer by asking if the range invoked will be consecutive or random.  Although a "Case" statement solution for FizzBuzz is generally more efficient, it will certainly be faster for a random range of numbers called.


I've approached several academics and professionals alike but nobody has challenged, so I would like to broaden the forum for additional contribution:

require 'benchmark'

def fizzbuzz(array)
 array.map!{ |number| 
 divisibleBy3 = (number % 3 == 0)
 divisibleBy5 = (number % 5 == 0)

 case
   when divisibleBy3 &amp;&amp; divisibleBy5   
    puts "FizzBuzz"
   when divisibleBy3
     puts "Fizz"
   when divisibleBy5
     puts "Buzz"
   else
   puts number
  end
 }
end

puts Benchmark.measure{fizzbuzz(Array(1..10000))}
puts "fizzbuzz"
# puts RubyVM::InstructionSequence.disasm(method(:fizzbuzz))

def super_fizzbuzz(array)
 array.map! { |number|
  if(number % 3 == 0 &amp;&amp; number % 5 == 0)
   puts "FizzBuzz" 
  elsif(number % 3 == 0)
   puts "Fizz"
  elsif (number % 5 == 0)
   puts "Buzz"
  else
  puts number
 end
 }
end

puts Benchmark.measure{super_fizzbuzz(Array(1..10000))}
puts "super_fizzbuzz"
# puts RubyVM::InstructionSequence.disasm(method(:super_fizzbuzz))

# Additional Documentation
 # https://en.wikipedia.org/wiki/Assembly_language
 # underTheHood =&gt; http://ruby-doc.org/core-2.0.0/RubyVM/InstructionSequence.html
 # http://ruby-doc.org/stdlib-2.0.0/libdoc/benchmark/rdoc/Benchmark.html


Upon executing the above, terminal-output correspondingly revealed the following when scraped with grep:




 Desktop  ruby fizzbuzz.rb | grep 0.0
 0.010000   0.000000   0.010000 (  0.010018)
 0.010000   0.000000   0.010000 (  0.011353)
Using a destructive update like map! is weird, the user does not expect his array to be mutated. I suggest one of two possibilities:


Replace map! with each and leave the rest of the code as is.
Replace map! with map, remove puts and handle printing out of the function. Adding a join("\n") may be needed.


The first is more obvious, the second one only has one responsability.



As a minor note, it is common style to use do end for multiline blocks, not {}.