[Rubycocoa-devel 961] Re: defining missing methods

Back to archive index

Laurent Sansonetti lsans****@apple*****
Thu Jun 7 03:56:46 JST 2007


After a quick investigation, it seems that #define_method is the  
culprit. It's really slow when used with Proc. Using a regular eval to  
add the method seems to be better.

$ cat bench2.rb
require 'benchmark'
class Foo
   def method_missing(*a); foo; end
   def foo; end
end
Foo.class_eval { define_method(:foo2) { 0 } }
Foo.class_eval("def foo3; end")
n = 1_000_000
Benchmark.bm(7) do |x|
   o = Foo.new
   x.report('method_missing') { n.times { o.missing } }
   x.report('define_method') { n.times { o.foo2 } }
   x.report('eval') { n.times { o.foo3 } }
   x.report('regular') { n.times { o.foo } }
end

$ ruby bench2.rb
              user     system      total        real
method_missing  1.030000   0.010000   1.040000 (  1.081370)
define_method  0.730000   0.010000   0.740000 (  0.773041)
eval     0.280000   0.000000   0.280000 (  0.303355)
regular  0.280000   0.000000   0.280000 (  0.296050)

Laurent

On Jun 5, 2007, at 5:47 PM, Laurent Sansonetti wrote:

> Hi,
>
> This afternoon I just tried a small experiment in the RubyCocoa  
> core. You know that every time you send a message from Ruby to  
> Objective-C, it goes through #method_missing, which is supposedly  
> slow.
>
> I experimented creating the missing method just after  
> #method_missing, so that #method_missing would be called only once  
> (the other messages would be "direct"). I thought it would be a good  
> performance gain.
>
> However, from a quick test, it doesn't seem to be better (it's even  
> sometimes worse).
>
> Any idea? I attached the patch and the bench test to this message.
>
> Laurent
>
> < 
> define_missing 
> .diff><bench.rb>_______________________________________________
> Rubycocoa-devel mailing list
> Rubyc****@lists*****
> http://lists.sourceforge.jp/mailman/listinfo/rubycocoa-devel




More information about the Rubycocoa-devel mailing list
Back to archive index