Project

General

Profile

Feature #4801 ยป bm.rb

Benchmark comparing hash and string keys for hashes with various hit rates - drbrain (Eric Hodel), 08/16/2011 03:18 AM

 
1
require 'benchmark'
2

    
3
string_hash = {}
4
symbol_hash = {}
5

    
6
string_keys = ('aaaaa'..'aaaaj').to_a
7

    
8
symbol_keys = string_keys.map { |key| key.to_sym }
9

    
10
string_keys.each_with_index do |key, index| string_hash[key] = index end
11
symbol_keys.each_with_index do |key, index| string_hash[key] = index end
12

    
13
test_string_keys = string_keys # 100% hit rate
14
test_symbol_keys = symbol_keys
15

    
16
TIMES = 500_000
17

    
18
puts '100% hit rate'
19
Benchmark.bm 24 do |bm|
20
  bm.report 'string hash, string keys' do
21
    TIMES.times do
22
      test_string_keys.each do |key|
23
        string_hash[key]
24
      end
25
    end
26
  end
27

    
28
  bm.report 'string hash, symbol keys' do
29
    TIMES.times do
30
      test_symbol_keys.each do |key|
31
        string_hash[key.to_s]
32
      end
33
    end
34
  end
35

    
36
  bm.report 'symbol hash, string keys' do
37
    TIMES.times do
38
      test_string_keys.each do |key|
39
        symbol_hash[key.to_sym]
40
      end
41
    end
42
  end
43

    
44
  bm.report 'symbol hash, symbol keys' do
45
    TIMES.times do
46
      test_symbol_keys.each do |key|
47
        symbol_hash[key]
48
      end
49
    end
50
  end
51
end
52

    
53
test_string_keys = ('aaaaa'..'aaaat').to_a # 50% hit rate
54
test_symbol_keys = test_string_keys.map do |key| key.to_sym end
55

    
56
puts ' 50% hit rate'
57
Benchmark.bm 24 do |bm|
58
  bm.report 'string hash, string keys' do
59
    TIMES.times do
60
      test_string_keys.each do |key|
61
        string_hash[key]
62
      end
63
    end
64
  end
65

    
66
  bm.report 'string hash, symbol keys' do
67
    TIMES.times do
68
      test_symbol_keys.each do |key|
69
        string_hash[key.to_s]
70
      end
71
    end
72
  end
73

    
74
  bm.report 'symbol hash, string keys' do
75
    TIMES.times do
76
      test_string_keys.each do |key|
77
        symbol_hash[key.to_sym]
78
      end
79
    end
80
  end
81

    
82
  bm.report 'symbol hash, symbol keys' do
83
    TIMES.times do
84
      test_symbol_keys.each do |key|
85
        symbol_hash[key]
86
      end
87
    end
88
  end
89
end
90

    
91
test_string_keys = ('aaaaa'..'aaazz').to_a # 1.5% hit rate
92
test_symbol_keys = test_string_keys.map do |key| key.to_sym end
93

    
94
puts '1.4% hit rate'
95
Benchmark.bm 24 do |bm|
96
  bm.report 'string hash, string keys' do
97
    TIMES.times do
98
      test_string_keys.each do |key|
99
        string_hash[key]
100
      end
101
    end
102
  end
103

    
104
  bm.report 'string hash, symbol keys' do
105
    TIMES.times do
106
      test_symbol_keys.each do |key|
107
        string_hash[key.to_s]
108
      end
109
    end
110
  end
111

    
112
  bm.report 'symbol hash, string keys' do
113
    TIMES.times do
114
      test_string_keys.each do |key|
115
        symbol_hash[key.to_sym]
116
      end
117
    end
118
  end
119

    
120
  bm.report 'symbol hash, symbol keys' do
121
    TIMES.times do
122
      test_symbol_keys.each do |key|
123
        symbol_hash[key]
124
      end
125
    end
126
  end
127
end
128