$ docker run --rm -v $(pwd):/app -w /app ruby:2.3.7 bash -c "gem install benchmark-ips; ruby /app/benchmark.rb"
Successfully installed benchmark-ips-2.7.2
1 gem installed
{:a=>"1", :b=>2, :c=>nil}
Warming up --------------------------------------
`Hash#[:a] || 3` (key exist)
148.791k i/100ms
`Hash#[:c] || 3` (key is nil)
141.521k i/100ms
`Hash#[:d] || 3` (key does not exist)
136.853k i/100ms
`Hash.fetch(:a) { 3 }` (key exist)
136.026k i/100ms
`Hash.fetch(:c) { 3 }` (key is nil)
133.084k i/100ms
`Hash.fetch(:d) { 3 }` (key does not exist)
121.019k i/100ms
`Hash.fetch(:a, 3)` (key exist)
126.041k i/100ms
`Hash.fetch(:c, 3)` (key is nil)
126.434k i/100ms
`Hash.fetch(:d, 3)` (key does not exist)
117.635k i/100ms
Calculating -------------------------------------
`Hash#[:a] || 3` (key exist)
5.080M (± 2.6%) i/s - 25.443M in 5.011909s
`Hash#[:c] || 3` (key is nil)
4.086M (± 3.6%) i/s - 20.521M in 5.029719s
`Hash#[:d] || 3` (key does not exist)
4.020M (± 1.4%) i/s - 20.117M in 5.005328s
`Hash.fetch(:a) { 3 }` (key exist)
4.928M (± 1.3%) i/s - 24.757M in 5.024695s
`Hash.fetch(:c) { 3 }` (key is nil)
4.919M (± 1.4%) i/s - 24.621M in 5.005663s
`Hash.fetch(:d) { 3 }` (key does not exist)
3.471M (± 1.4%) i/s - 17.427M in 5.021451s
`Hash.fetch(:a, 3)` (key exist)
4.003M (± 1.8%) i/s - 20.041M in 5.007797s
`Hash.fetch(:c, 3)` (key is nil)
4.170M (± 8.2%) i/s - 20.735M in 5.006985s
`Hash.fetch(:d, 3)` (key does not exist)
4.557M (± 5.3%) i/s - 22.821M in 5.023383s
Comparison:
`Hash#[:a] || 3` (key exist): 5080160.7 i/s
`Hash.fetch(:a) { 3 }` (key exist): 4927876.7 i/s - same-ish: difference falls within error
`Hash.fetch(:c) { 3 }` (key is nil): 4919496.8 i/s - same-ish: difference falls within error
`Hash.fetch(:d, 3)` (key does not exist): 4557139.1 i/s - 1.11x slower
`Hash.fetch(:c, 3)` (key is nil): 4169658.5 i/s - 1.22x slower
`Hash#[:c] || 3` (key is nil): 4085957.3 i/s - 1.24x slower
`Hash#[:d] || 3` (key does not exist): 4020012.1 i/s - 1.26x slower
`Hash.fetch(:a, 3)` (key exist): 4003104.0 i/s - 1.27x slower
`Hash.fetch(:d) { 3 }` (key does not exist): 3471148.5 i/s - 1.46x slower
Created
December 4, 2019 22:11
-
-
Save dmitryrck/17d65236b1620fba07d59bea6f799a3d 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/ips" | |
HASH = { a: "1", b: 2, c: nil } | |
puts HASH.inspect | |
Benchmark.ips do |x| | |
x.report("`Hash#[:a] || 3` (key exist)") { HASH[:a] || 3 } | |
x.report("`Hash#[:c] || 3` (key is nil)") { HASH[:c] || 3 } | |
x.report("`Hash#[:d] || 3` (key does not exist)") { HASH[:d] || 3 } | |
x.report("`Hash.fetch(:a) { 3 }` (key exist)") { HASH.fetch(:a) { 3 } } | |
x.report("`Hash.fetch(:c) { 3 }` (key is nil)") { HASH.fetch(:c) { 3 } } | |
x.report("`Hash.fetch(:d) { 3 }` (key does not exist)") { HASH.fetch(:d) { 3 } } | |
x.report("`Hash.fetch(:a, 3)` (key exist)") { HASH.fetch(:a, 3) } | |
x.report("`Hash.fetch(:c, 3)` (key is nil)") { HASH.fetch(:c, 3) } | |
x.report("`Hash.fetch(:d, 3)` (key does not exist)") { HASH.fetch(:d, 3) } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment