Created
June 24, 2013 14:49
-
-
Save jballanc/5850598 to your computer and use it in GitHub Desktop.
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' | |
class TestBench | |
VALUES = { first: "A", | |
second: "B", | |
third: "C", | |
fourth: "D" } | |
def check_with_map(val) | |
VALUES.fetch(val, "not found") | |
end | |
def check_with_case(val) | |
case val | |
when :first | |
"A" | |
when :second | |
"B" | |
when :third | |
"C" | |
when :fourth | |
"D" | |
else | |
"not found" | |
end | |
end | |
def check_with_ifs(val) | |
if :first === val | |
"A" | |
elsif :second === val | |
"B" | |
elsif :third === val | |
"C" | |
elsif :fouth === val | |
"D" | |
else | |
"not found" | |
end | |
end | |
end | |
Benchmark.bmbm do |x| | |
t = TestBench.new | |
vals = %w|first second third fourth fifth|.map(&:to_sym) | |
x.report("With Map") { 1_000_000.times { t.check_with_map(vals.sample) } } | |
x.report("With Case") { 1_000_000.times { t.check_with_case(vals.sample) } } | |
x.report("With Ifs") { 1_000_000.times { t.check_with_ifs(vals.sample) } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment