From: Intransition Date: 2010-10-08T15:41:31+09:00 Subject: Re: Benchmarks Project and example for `||=` On Oct 7, 9:27 pm, Ryan Davis wrote: > On Oct 7, 2010, at 15:11 , Intransition wrote: > > > However, if I am really concerned about squeezing out every bit of > > speed, I would do: > > >  class X > >    def initialize > >      @f = ( do_some_calc_or_lookup ) > >    end > >    def f > >      @f > >    end > >  end > > There is a damn good reason why lazy initializing exists in the first place, and it invalidates your statement above. How? > If you were __really__ concerned about squeezing out every bit of speed, you'd profile the code to see how it is used. If #f isn't being called in a tight loop, or isn't even being called much at all, then you might be spending more time running initialize than you would have doing the lazy initialize. You'll never know that mentarbating over micro-benchmarks. You assume I am not aware of how frequently X.new is bring called in comparison to X#f. In my case, its about 1 to N where N >= 1, and so forth for X#f2, X#f3, X#f4, etc. If #f wasn't being used but on rare occasion, I wouldn't worry about it. Moreover, by knowing the relative difference between the two, one can make a better effort at optimizing upfront, and potentially save time later. And yes I do indeed profile code when I am "__really__ concerned about squeezing out every bit of speed". In fact, if memory serves, I was doing exactly that when I was trying to decide if giving up the convenience of ||= was worth the performance gain.