This repository was archived by the owner on Nov 30, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ require 'benchmark/ips'
2
+
3
+ small_hash = { :key => true , :more_key => true , :other_key => true }
4
+ large_hash = ( 1 ...100 ) . inject ( { } ) { |hash , key | hash [ "key_#{ key } " ] = true ; hash }
5
+
6
+ Benchmark . ips do |x |
7
+ x . report ( 'keys.each with small hash' ) do
8
+ small_hash . keys . each { |value | value == true }
9
+ end
10
+
11
+ x . report ( 'each_key with small hash' ) do
12
+ small_hash . each_key { |value | value == true }
13
+ end
14
+
15
+ x . report ( 'keys.each with large hash' ) do
16
+ large_hash . keys . each { |value | value == true }
17
+ end
18
+
19
+ x . report ( 'each_key with large hash' ) do
20
+ large_hash . each_key { |value | value == true }
21
+ end
22
+ end
23
+
24
+ __END__
25
+
26
+ Calculating -------------------------------------
27
+ keys.each with small hash
28
+ 105.581k i/100ms
29
+ each_key with small hash
30
+ 112.045k i/100ms
31
+ keys.each with large hash
32
+ 7.625k i/100ms
33
+ each_key with large hash
34
+ 6.959k i/100ms
35
+ -------------------------------------------------
36
+ keys.each with small hash
37
+ 2.953M (± 3.8%) i/s - 14.781M
38
+ each_key with small hash
39
+ 2.917M (± 4.0%) i/s - 14.678M
40
+ keys.each with large hash
41
+ 79.349k (± 2.5%) i/s - 396.500k
42
+ each_key with large hash
43
+ 72.080k (± 2.1%) i/s - 361.868k
You can’t perform that action at this time.
0 commit comments