views:

440

answers:

6

In web app I am splitting strings and assigning to link names or to collections of strings. Is there a significant performance benefit to using stringbuilder for a web application?

EDIT: 2 functions: splitting up a link into 5-10 strings. THen repackaging into another string. Also I append one string at a time to a link everytime the link is clicked.

+4  A: 

Yes, concatenating regular strings is expensive (really appending on string on to the end of another). Each time a string is changed, .net drops the old string and creates a new one with the new values. It is an immutable object.

EDIT:

Stringbuilder should be used with caution, and evaluated like any other approach. Sometimes connactenting two strings together will be more efficient, and should be evaluated on a case by case basis.

Atwood has an interesting article related to this.

Kevin
Yes, yes, and yes! Especially when you have to do crazy form processing to generate an e-mail message or something along those lines.
Dillie-O
It really depends on what you're doing. We don't have much context here - encouraging the use of StringBuilder without thinking about it is a bad idea IMO. Plenty of people overuse StringBuilder and end up with less readable code with *worse* performance.
Jon Skeet
This shouldn't be so upvoted, regular concats are frequently *better*...
annakata
Indeed. But the real issue here is that the same rules apply to a webapp or anything else. The code works the same way and the reasons why you should (or shouldn't) use stringbuilder still apply.
Geoffrey Chetwood
+2  A: 

Why would the performance be any different in a web application or a winforms application?

Using stringbuilder is a matter of good practice because of memory and object allocation, the rules apply no matter why you are building the code.

Geoffrey Chetwood
+12  A: 

How many strings will you be concatenating? Do you know for sure how many there will be, or does it depend on how many records are in the database etc?

See my article on this subject for more details and guidelines - but basically, being in a web app makes no difference to how expensive string concatenation is vs using a StringBuilder.

EDIT: I'm afraid it's still not entirely clear from the question exactly what you're doing. If you've got a fixed set of strings to concatenate, and you can do it all in one go, then it's faster and probably more readable to do it using concatenation. For instance:

string faster = first + " " + second + " " + third + "; " + fourth;

string slower = new StringBuilder().Append(first)
                                   .Append(" ")
                                   .Append(second)
                                   .Append(" ")
                                   .Append(third)
                                   .Append("; ")
                                   .Append(fourth)
                                   .ToString();

Another alternative is to use a format string of course. This may well be the slowest, but most readable:

 string readable = string.Format("{0} {1} {2}; {3}",
                                 first, second, third, fourth);

The part of your question mentioning "adding a link each time" suggests using a StringBuilder for that aspect though - anything which naturally leads to a loop is more efficient (for moderate to large numbers) using StringBuilder.

Jon Skeet
bah, I was just going to reference your article anyway...
annakata
I'm not so sure the first one is faster. Isn't that really creating six temporary, intermediate string objects? I would imagine that one object creation and eight method calls would be faster than six object creations.
Glenn
Glenn, no I think it is, because the faster ex, is only creating one string at the end.
Kevin
and not taking four steps and a conversion to do it.
Kevin
It will create an array with the seven strings in, then call String.Concat.
Jon Skeet
+4  A: 

You should take a look at this excellent article by Jon Skeet about concatenating strings.

Anteru
what the hell? why was this downvoted?
annakata
Why would it be upvoted? It was posted after Jon Skeet's and has less information. Should we all copy Jon Skeet's link and hope to win upvotes for it?
Geoffrey Chetwood
Given the timing, I strongly suspect that Anteru was writing his answer at the same time I was writing mine.
Jon Skeet
not saying it should be upvoted (since it is just a dupe, but seems *very* harsh to downvote a dupe
annakata
@annakata: Feel free to cry about downvotes, but that is how the system works. I fail to see how downvotes are 'harsh' when they are worth -2 and an upvote is worth +10.
Geoffrey Chetwood
@Rich B - note that it's not annakata's answer.
Jon Skeet
@Jon Skeet: I am well aware. That is why it surprises me even more when people cry over downvotes to other answers.
Geoffrey Chetwood
@Rich B - "cry"? SO is supposed to be about answering questions. A negative score implies incorrectness, which is manifestly untrue here. It's bad for the health of SO in general if people are going to start downvoting valid answers.
annakata
When I finished it, the answer from Jon Skeet was not yet visible. I guess I was a few seconds faster.
Anteru
+2  A: 

If you're making the string in a loop with a high number of iterations, then it's a good idea to use stringbuilder. Otherwise, string concatenation is your best bet.

Jason Baker
+1  A: 

FIRSTLY, Are you still writing this application? If yes then STOP Performance tuning! SECONDLY, Prioritise Correctness over Speed. Readability is way more important in the long run for obvious reasons. THIRDLY, WE don't know the exact situation and code you are writing. We can't really advise you if this micro optimisation is important to the performance of your code or not. MEASURE the difference. I highly recommend Red Gate's Ants Profiler.

Harry
BTW i'm guilty of all of the above points! LOL
Harry