Created
August 28, 2019 04:07
-
-
Save tomfakes/639224fba1673180692f0ebfe864a563 to your computer and use it in GitHub Desktop.
Benchmark of different FizzBuzz implementations in Ruby
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/ips" | |
##### | |
def fizz_buzz_text(num) | |
result = '' | |
result += 'Fizz' if (num % 3).zero? | |
result += 'Buzz' if (num % 5).zero? | |
result.empty? ? num.to_s : result | |
end | |
##### | |
def fizz_buzz_array(num) | |
result = [] | |
result << 'Fizz' if (num % 3).zero? | |
result << 'Buzz' if (num % 5).zero? | |
result.empty? ? num.to_s : result.join | |
end | |
##### | |
def fizz_buzz_if_else(num) | |
if (num % 15) == 0 | |
"FizzBuzz" | |
elsif (num % 5) == 0 | |
"Fizz" | |
elsif (num % 3) == 0 | |
"Buzz" | |
else | |
num.to_s | |
end | |
end | |
##### | |
def fizz_buzz_if_return(num) | |
return "FizzBuzz" if (num % 15) == 0 | |
return "Fizz" if (num % 5) == 0 | |
return "Buzz" if (num % 3) == 0 | |
num.to_s | |
end | |
##### | |
def fizz_buzz_if_frozen(num) | |
if (num % 15) == 0 | |
"FizzBuzz".freeze | |
elsif (num % 5) == 0 | |
"Fizz".freeze | |
elsif (num % 3) == 0 | |
"Buzz".freeze | |
else | |
num.to_s | |
end | |
end | |
##### | |
def fizz_buzz_if_frozen_zero(num) | |
if (num % 15).zero? | |
"FizzBuzz".freeze | |
elsif (num % 5).zero? | |
"Fizz".freeze | |
elsif (num % 3).zero? | |
"Buzz".freeze | |
else | |
num.to_s | |
end | |
end | |
##### | |
Divisable_by_fifteen = Proc.new { |i| i % 15 == 0 } | |
Divisable_by_three = Proc.new { |i| i % 3 == 0 } | |
Divisable_by_five = Proc.new { |i| i % 5 == 0 } | |
def fizz_buzz_case(num) | |
case num | |
when Divisable_by_fifteen | |
"FizzBuzz" | |
when Divisable_by_five | |
"Fizz" | |
when Divisable_by_three | |
"Buzz" | |
else | |
num.to_s | |
end | |
end | |
##### Cached | |
VALUES = [] | |
def create_cache(max = 100) | |
1.upto(max).each { |i| VALUES[i] = fizz_buzz_if_frozen(i) } | |
end | |
def fizz_buzz_cached(num) | |
VALUES[num] | |
end | |
create_cache 100 | |
def fizz_buzz_cached_100 | |
(1..100).each { |x| fizz_buzz_cached x } | |
end | |
Benchmark.ips do |x| | |
x.config(:time => 5, :warmup => 2) | |
x.report("case") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_case x } | |
i += 1 | |
end | |
end | |
x.report("if - else") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_if_else x } | |
i += 1 | |
end | |
end | |
x.report("if - return") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_if_return x } | |
i += 1 | |
end | |
end | |
x.report("if - frozen") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_if_frozen x } | |
i += 1 | |
end | |
end | |
x.report("if - frozen - zero") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_if_frozen_zero x } | |
i += 1 | |
end | |
end | |
x.report("text") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_text x } | |
i += 1 | |
end | |
end | |
x.report("array") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_array x } | |
i += 1 | |
end | |
end | |
x.report("cached") do |times| | |
i = 0 | |
while i < times | |
(1..100).each { |x| fizz_buzz_cached x } | |
i += 1 | |
end | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment