Nasty string growth performance

Forgive my ignorance, but
may I know the reason why

buf += line

was made to act different from

buf << line

Of course, I prefer the syntax of the former w the behavior of the latter.
It’s commonplace (for nubies ie :).

kind regards -botp

···

Joel VanderWerf [mailto:vjoel@PATH.Berkeley.EDU] wrote:

Tom Trebisky wrote:

  buf = ""

            buf += line

and

  buf = Array.new

            buf.push line

are not comparable constructs.

The first one is the same as

buf = buf + line

which constructs a new string each time. The second one modifies the
existing array in place, which is why you saw better performance.

What you probably want is

buf << line

This just concatenates line on the end of the buf string,
modifying buf
in place.

Tom Trebisky wrote:
> buf = ""
...
> buf += line

What you probably want is

   buf << line

This just concatenates line on the end of the buf string,
modifying buf
in place.

Forgive my ignorance, but
may I know the reason why

   buf += line

was made to act different from

   buf << line

Of course, I prefer the syntax of the former w the behavior of the latter.
It's commonplace (for nubies ie :).

Maybe you've overseen the comment

modifying buf
in place.

regards
Karl-Heinz

···

In message "Nasty string growth performance" on 28.05.2004, Peña, Botp <botp@delmonte-phil.com> writes:

Joel VanderWerf [mailto:vjoel@PATH.Berkeley.EDU] wrote:

Peña, Botp wrote:

Forgive my ignorance, but
may I know the reason why

buf += line

was made to act different from

buf << line

Of course, I prefer the syntax of the former w the behavior of the latter.
It’s commonplace (for nubies ie :).

Perhaps you want

 buf += line

to be treated specially for strings. But it isn’t… it always means

 buf = buf + line

and I think you can see there that there is always a new object created.

+= is not a “real” operation… it’s only syntax sugar. It’s not a
method, so it can’t possibly modify an object in place.

Does this help any?

Hal