-
-
Save y8/fc52b8b10d7e832754d4 to your computer and use it in GitHub Desktop.
benchmark.rb
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' | |
puts RUBY_DESCRIPTION | |
hash = {foo: "bar", bar: "foo", baz: "bar", one: "five", five: "one"} | |
strings = ("a".."aa").to_a | |
symbols = strings.map(&:to_sym) | |
str_hash = Hash[strings.zip strings] | |
sym_hash = Hash[symbols.zip symbols] | |
Benchmark.ips do |x| | |
x.config(:time => 5, :warmup => 2) | |
x.report("hash[]") { hash[:foo] } | |
x.report("hash.has_key?") { hash.has_key? :foo } | |
x.report("str_hash[]") { str_hash['aq'] } | |
x.report("str_hash.has_key?") { str_hash.has_key? 'aq' } | |
x.report("sym_hash[]") { sym_hash[:aq] } | |
x.report("sym_hash.has_key?") { sym_hash.has_key? :aq } | |
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
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin14.0] | |
Calculating ------------------------------------- | |
hash[] 116679 i/100ms | |
hash.has_key? 116819 i/100ms | |
str_hash[] 104410 i/100ms | |
str_hash.has_key? 101510 i/100ms | |
sym_hash[] 116876 i/100ms | |
sym_hash.has_key? 114767 i/100ms | |
------------------------------------------------- | |
hash[] 7277455.9 (±14.2%) i/s - 35587095 in 4.999576s | |
hash.has_key? 6438265.3 (±15.4%) i/s - 31424311 in 5.004688s | |
str_hash[] 3883650.1 (±13.1%) i/s - 19107030 in 5.011662s | |
str_hash.has_key? 3747256.2 (±13.3%) i/s - 18474820 in 5.023986s | |
sym_hash[] 6189527.6 (±16.2%) i/s - 30154008 in 5.007450s | |
sym_hash.has_key? 5954672.6 (±15.2%) i/s - 29150818 in 5.013669s | |
rubinius 2.2.10 (2.1.0 bf61ae2e 2014-06-27 JI) [x86_64-darwin14.0.0] | |
Calculating ------------------------------------- | |
hash[] 215528 i/100ms | |
hash.has_key? 274802 i/100ms | |
str_hash[] 284426 i/100ms | |
str_hash.has_key? 285994 i/100ms | |
sym_hash[] 308111 i/100ms | |
sym_hash.has_key? 317849 i/100ms | |
------------------------------------------------- | |
hash[] 5245826.8 (±12.0%) i/s - 25863360 in 5.020344s | |
hash.has_key? 3920295.2 (±16.5%) i/s - 18961338 in 4.995880s | |
str_hash[] 4439750.5 (±6.8%) i/s - 22185228 in 5.021374s | |
str_hash.has_key? 4844793.1 (±7.5%) i/s - 24309490 in 5.046094s | |
sym_hash[] 5747593.2 (±7.5%) i/s - 28654323 in 5.017890s | |
sym_hash.has_key? 6226543.1 (±5.5%) i/s - 31149202 in 5.018689s | |
jruby 1.7.13 (1.9.3p392) 2014-06-24 43f133c on Java HotSpot(TM) 64-Bit Server VM 1.7.0_67-b01 [darwin-x86_64] | |
Calculating ------------------------------------- | |
hash[] 168115 i/100ms | |
hash.has_key? 228017 i/100ms | |
str_hash[] 197915 i/100ms | |
str_hash.has_key? 225676 i/100ms | |
sym_hash[] 216706 i/100ms | |
sym_hash.has_key? 227773 i/100ms | |
------------------------------------------------- | |
hash[] 13373023.5 (±15.8%) i/s - 64724275 in 4.977000s | |
hash.has_key? 13733368.8 (±14.3%) i/s - 67036998 in 4.986000s | |
str_hash[] 9787290.4 (±13.3%) i/s - 47895430 in 4.986000s | |
str_hash.has_key? 10548284.1 (±14.2%) i/s - 51679804 in 5.012000s | |
sym_hash[] 10999724.0 (±14.7%) i/s - 53743088 in 5.005000s | |
sym_hash.has_key? 11938579.8 (±15.1%) i/s - 58309888 in 5.004000s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment