Do you know where your variables are? PerlMonks

### Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)

by fergal (Chaplain)
 on Dec 12, 2006 at 13:49 UTC ( #589273=note: print w/replies, xml ) Need Help??

StringBuilder was "faster"

Apparently this can be true but only in certain circumstances. Really it just sounds like your coworkers read it somewhere and believed it.

• Comment on Re: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)

Replies are listed 'Best First'.
Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
by akho (Hermit) on Aug 12, 2007 at 15:41 UTC
One may do this with a logarithmic number of concatenations, though (instead of linear). I don't see a feasible way to do such a thing with StringBuilder,

That may make concatenation faster than StringBuilder even in the case of a large number of strings.

I wonder how Perl's join actually works.

How do you concatenate n strings in O(log n)? Every string will have to be concatenated to something least once, so it's not obvious to me how it could ever be less than O(n).

Concatenating in pairs and then concatenating the results etc adds up to O(n).

Oh.

Now I'm ashamed of myself.

Re^2: Five Ways to Reverse a String of Words (C#, Perl 5, Perl6, Ruby, Haskell)
by Anonymous Monk on Sep 08, 2008 at 22:21 UTC
It’s not about it being faster but more efficient. Each time you concatenate strings you cause new copies of the original string variable to be made.

So since faster is a relatively objective term and "efficient" depends on what you're trying to minimise usage of, arguing that it's more efficient without further details doesn't really work.

Of course you could define efficiency differently. Maybe you're trying to minimise peak memory consumption, network utilisation, coding time or whatever but you haven't said that's what you want.

In this case, why do you want to avoid the string copying? Because each copy takes time. Unless the final string is long enough to start filling main memory, memory usage is not an issue and that leaves only speed. So as far as I can see, except for special cases, when discussing string concat the only useful definition of "more efficient" == "faster".

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://589273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-01-16 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The STEM quote I most wish I'd made is:

Results (153 votes). Check out past polls.

Notices?