From: Paul Brannan Date: 2001-10-10T03:37:56+09:00 Subject: [ruby-talk:22332] Re: include vs. extend On Wed, 10 Oct 2001, Yukihiro Matsumoto wrote: > There's no case that the latter is faster, I think. > > matz. The latter is faster in the following case (Ruby 1.6.5): N = 1000000 module M1; def foo; end; end module M2; def bar; end; end module M3; def xyz; end; end module M4; def abc; end; end class Foo include M1 include M2 include M3 include M4 end class Bar def initialize extend M1 extend M2 extend M3 extend M4 end end start = Time.now x = Foo.new N.times { x.foo; x.bar; x.xyz; x.abc; } puts Time.now - start start = Time.now x = Bar.new N.times { x.foo; x.bar; x.xyz; x.abc; } puts Time.now - start #=> 3.197413 (include) #=> 3.15327 (extend) It is not much faster, but it is consistently faster. Actually, now that I give it a second look, I am not sure that my tests are correct. 1) The above test, with the tests in reverse order (extend is faster): #=> 3.149055 (extend) #=> 3.195228 (include) 2) The above test, with GC.start surrounding (outside of) each test (include is faster): #=> 3.141431 (include) #=> 3.189973 (extend) 3) The above test, with GC.start surrounding each test, and the tests in reverse order (extend is faster): #=> 3.158805 (extend) #=> 3.168021 (include) So in 3/4 cases, extend is (consistently) faster. Why is this? Paul