From: dkmd_nielsen Date: 2008-10-16T05:24:42+09:00 Subject: Re: Lame question about instance variable and attribute On Oct 15, 10:40 am, Bryan D wrote: > On Oct 15, 7:41 am, dkmd_nielsen wrote: > > > Assuming an attribute :field is defined, is there a processing > > difference between using @field and self.field [within the defining > > class?]  Does the ruby interpretter take a different path in yielding > > the value depending how it is referenced?  I haven't notice anything. > > I'm just curious if the interpretter takes extra steps when yielding > > self.field. > > > Thanks > > In terms of speed, it's very close, but using @field is slightly > faster, at least on my system. > > Rehearsal ----------------------------------------------- > using self:   4.640000   2.350000   6.990000 (  7.032596) > using @:      4.550000   2.350000   6.900000 (  6.928132) > ------------------------------------- total: 13.890000sec > >                   user     system      total        real > using self:   4.660000   2.380000   7.040000 (  7.137048) > using @:      4.570000   2.360000   6.930000 (  7.075875) > > Benchmark code: > require 'benchmark' > > class Dummy >   attr_reader :foo >   def initialize >     @foo = 7983.3435 >   end > >   def using_self >     self.foo >   end > >   def using_direct >     @foo >   end > end > > n = 1000000 > @d = Dummy.new > Benchmark.bmbm(7) do |x| >   x.report("using self:") { n.times do @d.using_self;   end } >   x.report("using @:")    { n.times do @d.using_direct; end } > end Thanks for the commentary, guys. This is the kind of stuff that is useful to me. Most everything I do i very iterative. Fortunately, if you can call it fortunate, most of my bottlenecks have nothing to do with Ruby. Most of the time is I/O related stuff between servers. Thanks again, Happy Halloween