On 7/15/07, SonOfLilit <sonoflilit / gmail.com> wrote:
> > I wasn't even aware that "foo = gets; foo.chomp!" would be more efficient
> > than "foo = gets.chomp" -- that's news to me, and I'm not sure I get why
> > that should be the case at the moment.
Hmm it should not, but I am quite sure it is right now, maybe a
language guru can tell me better, but I am afraid that the allocation
of memory for a temporary copy of the string returned by gets is not
optimized away.
Naively I think this will happen
tmp1 = gets
tmp2 = tmp1.dup
tmp2.chomp!
foo = tmp2

and not
foo = gets
foo.chomp!

Anybody know how this is compiled by YARV?
> > Even so, I tend to agree --
> > except in very limited circumstances, I think choosing the former over
> > the latter because of efficiency is a case of premature optimization.
I do not agree precisely, efficiency is a case of premature
optimization iff there is a price to pay for it, and I think there is
in our case...
When all things are equal I prefer the efficent code e.g.
s << "a"
instead of
s += "a"

Robert


-- 
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck