From: Christoph Date: 2005-05-18T05:20:12+09:00 Subject: Re: Extending an Instance only Once peter.vanbroekhoven@gmail.com schrieb: >You made a typo in your tests (x should be replaced with f), but it >does not influence the result. The only explanation I have is that the >test used in include is more complicated than the one used in #is_a? >because include doesn't only check the module passed as parameter to >#include, it checks modules included in that module too. I guess even >when there are no such modules, there is still some overhead (it's not >exactly a straightforward algorithm). But unless you really intend to >do 500000 extends in a row, I don't think that is really a problem... >:-) > > The explanation is probably simpler - f.extend(M) makes hook calls to M.extend_object(f) and M.extended(f) - M.extend_object(f) probably tests if f.is_a?(M) a - this means its 4 method calls, versus 1 call + 1 if statement ... you'll probably find a much smaller difference calling M.extend_object(f) only. --- require 'benchmark' include Benchmark module M class Bar < Array; end class Foo < Bar; end n = 500000 bm(15){ |x| f = Foo.new x.report( "Test First" ){ n.times{ extend_object( f ) unless f.is_a?( M ) } } f = Foo.new x.report( "Always Extend" ){ n.times{ extend_object( f ) } } } end --- /Christoph >Peter > > > > >