##################### # Ruby Version: 2.1.6 ##################### # gem 'activesupport', '=3.2.22' # gem 'activesupport', '=4.2.5' # gem 'activesupport', '=7.2.1' require 'active_support/all' require 'benchmark/ips' Benchmark.ips do |x| x.config(time: 5, warmup: 2) @hash_with_indifferent_access = ActiveSupport::HashWithIndifferentAccess.new(a: 1) @hash = Hash[a: 2] ###### # Hash ###### x.report("Common Hash#fetch access") do @hash.fetch :a end x.report("Common Hash#[] access") do @hash[:a] end ########################### # HashWithIndifferentAccess ########################### x.report("HashWithIndifferentAccess#fetch Symbol") do @hash_with_indifferent_access.fetch :a end x.report("HashWithIndifferentAccess#fetch String") do @hash_with_indifferent_access.fetch 'a' end x.report("HashWithIndifferentAccess #[] Symbol") do @hash_with_indifferent_access[:a] end x.report("HashWithIndifferentAccess #[] String") do @hash_with_indifferent_access['a'] end ######################################### # Hash#with_indifferent_access convertion ######################################### x.report("Hash#with_indifferent_access #fetch Symbol") do @hash.with_indifferent_access.fetch :a end x.report("Hash#with_indifferent_access #fetch String") do @hash.with_indifferent_access.fetch 'a' end x.report("Hash#with_indifferent_access #[] Symbol") do @hash.with_indifferent_access[:a] end x.report("Hash#with_indifferent_access #[] String") do @hash.with_indifferent_access['a'] end # Compare the iterations per second of the various reports! x.compare! end