Created
February 11, 2020 19:53
-
-
Save drbrain/82a7d7947344584f96458bd9322f148d to your computer and use it in GitHub Desktop.
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
$ ruby -v t.rb | |
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19] | |
Warming up -------------------------------------- | |
if else 186.397k i/100ms | |
case with comparison 149.445k i/100ms | |
case all range 127.260k i/100ms | |
case hash cheat 190.074k i/100ms | |
Calculating ------------------------------------- | |
if else 3.447M (± 7.5%) i/s - 17.149M in 5.007151s | |
case with comparison 2.388M (±18.2%) i/s - 11.657M in 5.065894s | |
case all range 2.416M (±11.9%) i/s - 11.962M in 5.045475s | |
case hash cheat 4.460M (±14.9%) i/s - 21.668M in 5.005362s | |
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/ips' | |
Benchmark.ips do |bm| | |
bm.report 'if else' do | |
x = rand(1..20) | |
if (1..5) === x | |
"It's between 1 and 5" | |
elsif x == 6 | |
"It's 6" | |
elsif x > 10 | |
"It's above 10" | |
else | |
"It's probably 7, 8 or 9" | |
end | |
end | |
bm.report 'case with comparison' do | |
x = rand(1..20) | |
case x | |
when 1..5 | |
"It's between 1 and 5" | |
when 6 | |
"It's 6" | |
when x>10 | |
"It's above 10" | |
else | |
"It's probably 7, 8 or 9" | |
end | |
end | |
bm.report 'case all range' do | |
x = rand(1..20) | |
case x | |
when 1..5 | |
"It's between 1 and 5" | |
when 6 | |
"It's 6" | |
when 7..9 | |
"It's probably 7, 8 or 9" | |
else | |
"It's above 10" | |
end | |
end | |
bm.report 'case hash cheat' do | |
x = rand(1..20) | |
case x | |
when 1, 2, 3, 4, 5 | |
"It's between 1 and 5" | |
when 6 | |
"It's 6" | |
when 7, 8, 9 | |
"It's probably 7, 8 or 9" | |
else | |
"It's above 10" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment