Skip to content

Instantly share code, notes, and snippets.

@arn-e
Last active December 21, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arn-e/6299127 to your computer and use it in GitHub Desktop.
Save arn-e/6299127 to your computer and use it in GitHub Desktop.
integer square root 'calculation' for ints between 1..255
require 'benchmark'
def sqrt_calc
sqrt_hash = integer_sqrt_hash # instantiating hash table
n = 30000000 # number of test cycles to benchmark
Benchmark.bm do |x|
x.report do
n.times do
rgb_val = rand(1..255);
int_sqrt = sqrt_hash[rgb_val];
# puts "#{rgb_val}, #{int_sqrt}"
end
end
end
end
# Benchmark Results
# user system total real
# 3.360000 0.000000 3.360000 ( 3.356045)
def integer_sqrt_hash
{
1 => 1.00,
2 => 1.41,
3 => 1.73,
4 => 2.00,
5 => 2.24,
6 => 2.45,
7 => 2.65,
8 => 2.83,
9 => 3.00,
10 => 3.16,
11 => 3.32,
12 => 3.46,
13 => 3.61,
14 => 3.74,
15 => 3.87,
16 => 4.00,
17 => 4.12,
18 => 4.24,
19 => 4.36,
20 => 4.47,
21 => 4.58,
22 => 4.69,
23 => 4.80,
24 => 4.90,
25 => 5.00,
26 => 5.10,
27 => 5.20,
28 => 5.29,
29 => 5.39,
30 => 5.48,
31 => 5.57,
32 => 5.66,
33 => 5.74,
34 => 5.83,
35 => 5.92,
36 => 6.00,
37 => 6.08,
38 => 6.16,
39 => 6.24,
40 => 6.32,
41 => 6.40,
42 => 6.48,
43 => 6.56,
44 => 6.63,
45 => 6.71,
46 => 6.78,
47 => 6.86,
48 => 6.93,
49 => 7.00,
50 => 7.07,
51 => 7.14,
52 => 7.21,
53 => 7.28,
54 => 7.35,
55 => 7.42,
56 => 7.48,
57 => 7.55,
58 => 7.62,
59 => 7.68,
60 => 7.75,
61 => 7.81,
62 => 7.87,
63 => 7.94,
64 => 8.00,
65 => 8.06,
66 => 8.12,
67 => 8.19,
68 => 8.25,
69 => 8.31,
70 => 8.37,
71 => 8.43,
72 => 8.49,
73 => 8.54,
74 => 8.60,
75 => 8.66,
76 => 8.72,
77 => 8.77,
78 => 8.83,
79 => 8.89,
80 => 8.94,
81 => 9.00,
82 => 9.06,
83 => 9.11,
84 => 9.17,
85 => 9.22,
86 => 9.27,
87 => 9.33,
88 => 9.38,
89 => 9.43,
90 => 9.49,
91 => 9.54,
92 => 9.59,
93 => 9.64,
94 => 9.70,
95 => 9.75,
96 => 9.80,
97 => 9.85,
98 => 9.90,
99 => 9.95,
100 => 10.00,
101 => 10.05,
102 => 10.10,
103 => 10.15,
104 => 10.20,
105 => 10.25,
106 => 10.30,
107 => 10.34,
108 => 10.39,
109 => 10.44,
110 => 10.49,
111 => 10.54,
112 => 10.58,
113 => 10.63,
114 => 10.68,
115 => 10.72,
116 => 10.77,
117 => 10.82,
118 => 10.86,
119 => 10.91,
120 => 10.95,
121 => 11.00,
122 => 11.05,
123 => 11.09,
124 => 11.14,
125 => 11.18,
126 => 11.22,
127 => 11.27,
128 => 11.31,
129 => 11.36,
130 => 11.40,
131 => 11.45,
132 => 11.49,
133 => 11.53,
134 => 11.58,
135 => 11.62,
136 => 11.66,
137 => 11.70,
138 => 11.75,
139 => 11.79,
140 => 11.83,
141 => 11.87,
142 => 11.92,
143 => 11.96,
144 => 12.00,
145 => 12.04,
146 => 12.08,
147 => 12.12,
148 => 12.17,
149 => 12.21,
150 => 12.25,
151 => 12.29,
152 => 12.33,
153 => 12.37,
154 => 12.41,
155 => 12.45,
156 => 12.49,
157 => 12.53,
158 => 12.57,
159 => 12.61,
160 => 12.65,
161 => 12.69,
162 => 12.73,
163 => 12.77,
164 => 12.81,
165 => 12.85,
166 => 12.88,
167 => 12.92,
168 => 12.96,
169 => 13.00,
170 => 13.04,
171 => 13.08,
172 => 13.11,
173 => 13.15,
174 => 13.19,
175 => 13.23,
176 => 13.27,
177 => 13.30,
178 => 13.34,
179 => 13.38,
180 => 13.42,
181 => 13.45,
182 => 13.49,
183 => 13.53,
184 => 13.56,
185 => 13.60,
186 => 13.64,
187 => 13.67,
188 => 13.71,
189 => 13.75,
190 => 13.78,
191 => 13.82,
192 => 13.86,
193 => 13.89,
194 => 13.93,
195 => 13.96,
196 => 14.00,
197 => 14.04,
198 => 14.07,
199 => 14.11,
200 => 14.14,
201 => 14.18,
202 => 14.21,
203 => 14.25,
204 => 14.28,
205 => 14.32,
206 => 14.35,
207 => 14.39,
208 => 14.42,
209 => 14.46,
210 => 14.49,
211 => 14.53,
212 => 14.56,
213 => 14.59,
214 => 14.63,
215 => 14.66,
216 => 14.70,
217 => 14.73,
218 => 14.76,
219 => 14.80,
220 => 14.83,
221 => 14.87,
222 => 14.90,
223 => 14.93,
224 => 14.97,
225 => 15.00,
226 => 15.03,
227 => 15.07,
228 => 15.10,
229 => 15.13,
230 => 15.17,
231 => 15.20,
232 => 15.23,
233 => 15.26,
234 => 15.30,
235 => 15.33,
236 => 15.36,
237 => 15.39,
238 => 15.43,
239 => 15.46,
240 => 15.49,
241 => 15.52,
242 => 15.56,
243 => 15.59,
244 => 15.62,
245 => 15.65,
246 => 15.68,
247 => 15.72,
248 => 15.75,
249 => 15.78,
250 => 15.81,
251 => 15.84,
252 => 15.87,
253 => 15.91,
254 => 15.94,
255 => 15.97
}
end
sqrt_calc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment