My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
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.

Update: Chad Perrin notices that String#<< and String#concat 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

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

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)


Subcribe to this comment thread
Remember my details

Picture of me

.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Agile Manifesto & Principles
Principles Of OOD
Ruby on Rails

RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner