report("method_missing") do
1_000_000.times { t.test_unknown }
end
end
Note what we are not testing: we are not comparing the raw speed of ordinary
method dispatch versus a bare call to method_missing. We are comparing an ordinary
method call to the standard Ruby practice of using method_missing to answer for
one method name. This gives us answers that are more relevant to our question:
???How much will method_missing hurt me in this particular piece of code????
We use the Benchmark.bmbm method, which runs the entire benchmark suite once (the
???rehearsal???) to minimize startup costs and give the measured code a ???warm start.???
To get the most accurate numbers possible, each trial runs one million method calls.
The Benchmark library starts garbage collection before each run, because garbage
collection during the measured run would alter the results. Here is the output of that
benchmark on my computer:
Rehearsal ------------------------------------------------------
Direct method call 0.350000 0.000000 0.350000 ( 0.352929)
method_missing 0.480000 0.000000 0.480000 ( 0.476009)
--------------------------------------------- total: 0.830000sec
user system total real
Direct method call 0.320000 0.000000 0.320000 ( 0.324030)
method_missing 0.480000 0.000000 0.480000 ( 0.477420)
The rehearsal numbers come first, followed by the actual measurement. We can see
that under this environment, the average cost of a normal method call is 320 nanoseconds
(0.
Pages:
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237