From: peter.vanbroekhoven@... Date: 2005-05-18T00:50:31+09:00 Subject: Re: Extending an Instance only Once > Hrm...I see code that looks like that's what's going on inside of > class.c, but apparently it is faster to test myself: > > > require 'benchmark' > include Benchmark > > module M; end > class Bar < Array; end > class Foo < Bar; end > > n = 500000 > bm(15){ |x| > f = Foo.new > x.report( "Always Extend" ){ > n.times{ > x.extend( M ) ^^^ > } > } > > f = Foo.new > x.report( "Test First" ){ > n.times{ > x.extend( M ) unless x.is_a?( M ) ^^^ ^^^ > } > } > } > > user system total real > Always Extend 1.610000 0.010000 1.620000 ( 1.983911) > Test First 0.730000 0.000000 0.730000 ( 0.745857) > > > Is there a flaw with my test above? 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... :-) Peter