_J Blog

学んだ事のまとめやメモなどが中心です

method_missingの速度計測

遅くなるとは聞いていたが、どんなもんか興味で単純な関数呼び出しでのベンチマークをしてみた。

method_missing と 通常呼び出しの検証結果

user system total real
通常呼び出し 0.000000 0.000000 0.000000 0.000029
method_missing 0.000000 0.000000 0.000000 0.000056

ざっくり見てみたところ
method_missing を利用したときは通常呼び出しの約2倍程度遅い


下記ベンチマークコード(ruby1.9.2-p180)

require 'benchmark'
require 'pp'
class MethodTest
  def method_missing(method_name, *args)
    args
  end
  def test(test)
    return test
  end
end
a = MethodTest.new
puts Benchmark::CAPTION
pp Benchmark.measure {
  10000.times do |i|
    a.test('pool')
  end
}
pp Benchmark.measure {
  10000.times { |i| a.hoge('pool')
  }
}