Created
August 31, 2012 13:45
-
-
Save jonleighton/3552829 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' | |
h = { foo: :bar } | |
Benchmark.ips do |r| | |
r.report('#[]') { h[:foo] } | |
r.report('#fetch') { h.fetch(:foo) } | |
end |
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
Calculating ------------------------------------- | |
#[] 84463 i/100ms | |
#fetch 58818 i/100ms | |
------------------------------------------------- | |
#[] 3004469.2 (±1.6%) i/s - 15034414 in 5.005338s | |
#fetch 2196062.9 (±0.8%) i/s - 10998966 in 5.008783s |
does not look like significanly slower anymore
~ $ ruby hash_access_benchmark.rb
Calculating -------------------------------------
#[] 216.732k i/100ms
#fetch 217.366k i/100ms
-------------------------------------------------
#[] 11.665M (± 1.7%) i/s - 58.301M
#fetch 10.387M (± 0.6%) i/s - 51.950M
~ $ ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like rb_hash_fetch_m winds up calling rb_scan_args and rb_block_given_p where rb_hash_aref does not. rb_block_given_p could move into the conditional which evaluates it if one doesn't care about the warning. I don't know what you could do about parsing the varargs though.