Categories
Archives
Emphasize Selected Radio Options with jQuery | Home | Eliminate Interface Clutter With Arc90 Lab's Collapsible Panel! (As Seen On TV)
Filed under Quick Tips on March 17, 2008 by Patrick ForceString Concatenation vs. StringBuilder
When developing applications, a common concern often creates a battle in the mind of the developer: How can I write this to make it easier to maintain, versus how can I optimize performance?
The debate of string concatenation via the + operator vs. StringBuilder in Java illustrates one tip on how to approach the higher level argument in this particular case:
String s = "";
for(i = 0; i <= 10000; i++)
{
s += " word";
}
vs.
StringBuilder stringBuilder = new StringBuilder();
for(i = 0; i <= 10000; i++)
{
stringBuilder.append(" word");
}
In most every case, using StringBuilder will prove faster than the string concatenation. Performing concatenations 5-10 times and above in a single operation may justify using StringBuilder for performance reasons, but never so much as to sacrifice clean code or the easier ability to manage what you've written. For instance, consider the following:
String s = "<Customers>" +
"<Customer>" +
"<Name>Steven Jones</Name>" +
"<Address>123 Greenwich St.</Address>" +
"</Customer>" +
"<Customer>" +
"<Name>Gary Hall</Name>" +
"<Address>54 Rector St.</Address>" +
"</Customer>" +
"</Customers>";
For this particular case, I'd drastically prefer to write and work with the above piece of code before having to deal with the alternative w/ StringBuilder:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<Customers>");
stringBuilder.append("<Customer>");
stringBuilder.append("<Name>Steven Jones</Name>");
stringBuilder.append("<Address>123 Greenwich St.</Address>");
stringBuilder.append("</Customer>");
stringBuilder.append("<Customer>");
stringBuilder.append("<Name>Gary Hall</Name>");
stringBuilder.append("<Address>54 Rector St.</Address>");
stringBuilder.append("</Customer>");
stringBuilder.append("</Customers>");
String s = stringBuilder.toString();
In fact, I can't believe I even finished the StringBuilder example because it felt like such a waste of time, not to mention for this particular case of an XML string, it's not nearly as friendly to traverse through with StringBuilder.
Basically StringBuilder can produce performance gains (somewhat noticeable at very large iterations), but a developer should base his decision of whether to use + concatenation or StringBuilder primarily on ease of implementation and maintenance.
Trackback Pings (TrackBack URL for this entry)
http://www.arc90.com/cgi-bin/mt4/mt-tb.cgi/115.
Comments
Good point Gene. I certainly think there are development considerations that aren't most appropriately approached "primarily on ease of implementation." The point here, however, is to suggest approaching certain development tasks using that premise, while always keeping an eye on things like performance considerations, potential pitfalls in functionality, etc. It's a way, I think, to consider both developer performance and machine performance, and to balance these things at all times.
Posted on May 30, 2008 12:09 PM by Patrick
Emphasize Selected Radio Options with jQuery | Main | Eliminate Interface Clutter With Arc90 Lab's Collapsible Panel! (As Seen On TV)

>>primarily on ease of implementation.
yeah right.
Who was it that said "Let's just use the last two digits of the year because it's easier for us right now"?
Posted on May 30, 2008 9:16 AM by Gene