File: keys_each_vs_each_key.rb

package info (click to toggle)
ruby-rspec 3.12.0c0e1m1s0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,752 kB
  • sloc: ruby: 69,818; sh: 1,861; makefile: 99
file content (43 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'benchmark/ips'

small_hash = { :key => true, :more_key => true, :other_key => true }
large_hash = (1...100).inject({}) { |hash, key| hash["key_#{key}"] = true; hash }

Benchmark.ips do |x|
  x.report('keys.each with small hash') do
    small_hash.keys.each { |value| value == true }
  end

  x.report('each_key with small hash') do
    small_hash.each_key { |value| value == true }
  end

  x.report('keys.each with large hash') do
    large_hash.keys.each { |value| value == true }
  end

  x.report('each_key with large hash') do
    large_hash.each_key { |value| value == true }
  end
end

__END__

Calculating -------------------------------------
keys.each with small hash
                       105.581k i/100ms
each_key with small hash
                       112.045k i/100ms
keys.each with large hash
                         7.625k i/100ms
each_key with large hash
                         6.959k i/100ms
-------------------------------------------------
keys.each with small hash
                          2.953M (± 3.8%) i/s -     14.781M
each_key with small hash
                          2.917M (± 4.0%) i/s -     14.678M
keys.each with large hash
                         79.349k (± 2.5%) i/s -    396.500k
each_key with large hash
                         72.080k (± 2.1%) i/s -    361.868k