-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecode_bench.rb
More file actions
161 lines (139 loc) · 3.5 KB
/
decode_bench.rb
File metadata and controls
161 lines (139 loc) · 3.5 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'benchmark/ips'
KEYS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
KEYS_HASH = KEYS.each_char.with_index.inject({}){|h,(k,v)| h[k]=v;h}
BASE = KEYS.length
### Decode Method Candidates =====
# The one that is used in base62-rb library
# Use 'while' loop, and define a variable i as the counter
def decode(str)
num = 0
i = 0
len = str.length - 1
while i < str.length
pow = BASE ** (len - i)
num += KEYS_HASH[str[i]] * pow
i += 1
end
return num
end
# Use KEYS.index to get the index
# instead of using a predefined Hash Map(KEYS_HASH)
def decode1(str)
num = 0
i = 0
while i < str.length
pow = BASE ** (str.length - i -1)
num += KEYS.index(str[i]) * pow
i += 1
end
return num
end
# Use 'for...in...' loop
def decode2(str)
num = 0
for i in 0...str.length
pow = BASE ** (str.length - i -1)
num += KEYS_HASH[str[i]] * pow
end
return num
end
# Use String#each_char, and #with_index
# Use block to do the calcuation
def decode3(str)
num = 0
str.each_char.with_index do |char, i|
pow = BASE ** (str.length - i -1)
num += KEYS.index(char) * pow
end
return num
end
# Similar as above, but use KEYS_HASH to get the index
def decode4(str)
num = 0
str.each_char.with_index do |char, i|
pow = BASE ** (str.length - i -1)
num += KEYS_HASH[char] * pow
end
return num
end
# Similar as above, but explicitly define the counter
# without using #with_index
def decode5(str)
num = 0
i = 0
str.each_char do |char|
pow = BASE ** (str.length - i -1)
num += KEYS_HASH[char] * pow
i += 1
end
return num
end
# same thing but with less variable
def decode6(str)
num = 0
i = 0
while i < str.length
num += KEYS_HASH[str[i]] * (BASE ** (str.length - 1 - i))
i += 1
end
return num
end
### ==================
### Testing =====
tests = ["A", "Jr", "DFL", "2B5S", "8zTZmv", "1AnE6bpNA", "hjNv8tS3K"]
Benchmark.ips do |x|
x.report("decode") do
tests.each do |test|
decode(test)
end
end
x.report("decode1") do
tests.each do |test|
decode1(test)
end
end
x.report("decode2") do
tests.each do |test|
decode2(test)
end
end
x.report("decode3") do
tests.each do |test|
decode3(test)
end
end
x.report("decode4") do
tests.each do |test|
decode4(test)
end
end
x.report("decode5") do
tests.each do |test|
decode5(test)
end
end
x.report("decode6") do
tests.each do |test|
decode6(test)
end
end
end
### Results
# Results tested on Macbook Air 1.4 GHz Intel Core i5, 8 GB 1600 MHz DDR3
# Using Ruby 2.1.3p242
# Calculating -------------------------------------
# decode 6677 i/100ms
# decode1 5962 i/100ms
# decode2 5535 i/100ms
# decode3 4378 i/100ms
# decode4 4502 i/100ms
# decode5 5915 i/100ms
# decode6 6443 i/100ms
# -------------------------------------------------
# decode 67891.4 (±9.7%) i/s - 340527 in 5.069726s
# decode1 62385.4 (±7.8%) i/s - 310024 in 5.000584s
# decode2 57033.8 (±8.5%) i/s - 287820 in 5.082882s
# decode3 45356.5 (±6.5%) i/s - 227656 in 5.041103s
# decode4 47708.3 (±6.5%) i/s - 238606 in 5.023164s
# decode5 64856.4 (±6.6%) i/s - 325325 in 5.038748s
# decode6 67880.8 (±7.4%) i/s - 341479 in 5.059089s