Blaine Buxton has a nice post on Promises And String Concatenation
In that post, Blaine notes that string concatenation "make[s] your code slow and consumes memory," and that you are often told to use something like StringBuilder
(in Java) when doing a lot of string concatenations.
His position is that the language should abstract that for us and "do the right thing" when you use
(or the concatenation operator). Using + as a message to an object, it would be possible for us mere programmers
to implement a string builder on top of + to really speed it up. Of course that's not the case in Java, but he shows an implementation of it in Ruby that, over 1 million iterations of concatenating 'a' to a string, takes only 23.8 seconds versus 4500+ the "normal way."
I'd like to see benchmarks in normal usage to see if that speed increase is typical in a system that uses tons of concatenation, but those numbers are still staggering.
And I agree the language should do it for us, assuming it is possible. I add the clause about possibility there because I can't comprehend why it hasn't been done to begin with (nor have I thought a lot about it). Good catch Blaine.
Chad Perrin notices
do the same thing.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
One place that I love to use the StringBuffer where I don't think you could *ever* do implicitly in a language that passes Strings by value is in recursion. Sometimes, I will have a recursive function that keeps passing a StringBuffer in the recursive call to build some data. The benefit of course is that the StringBuffer gets passed by reference where as a string object gets passed by value.
Of course, this is a rare and specialized case. I would also love is standard string concatenation was just faster in general.
Posted by Ben Nadel
on Aug 25, 2007 at 01:07 PM UTC - 5 hrs
Ben, I'm not clear on why it wouldn't be possible to do that passing by value. (Or maybe I'm not clear on what you're doing!) =)
What about making the function return the modified string?
On the note about by-value and by-reference: I see there may be trouble in Java if they were to implement this with strings and string buffers and the way they are passed into functions, but I imagine they could just change the way concatenation works on Strings to be more like what happens with StringBuffers in the backend.
Posted by Sam
on Aug 25, 2007 at 05:01 PM UTC - 5 hrs
Leave a comment
For instance, and this is just off the top of my head, let's say I had a query that contained a recurse(d) CFDIrectory query and I want to build an XML string that represented the directory structure. I could create a StringBuffer and then create a function that keeps calling itself passing in the CFDirectory query object, the parent directory (used for a query of queries), and the StringBuffer that I am using to build the XML string.
In this case, the function would NOT return any value (which should be faster and more memory efficient since CF doesn't have to carve out a return value alocation (at least that is what I am told). However, since the StringBuffer is passed by reference, once all the recursion is done, the StringBuffer should hold a full XML string representation of the CFDirectory query.
Not sure if that is clear :)
Posted by Ben Nadel
on Aug 25, 2007 at 05:15 PM UTC - 5 hrs