zsharp
zsharp

Reputation: 13756

Is it worth using StringBuilder in web apps?

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.

Upvotes: 2

Views: 1046

Answers (6)

kemiller2002
kemiller2002

Reputation: 115508

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.

Upvotes: 4

Andrew Harry
Andrew Harry

Reputation: 13909

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1501656

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.

Upvotes: 12

Jason Baker
Jason Baker

Reputation: 198657

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.

Upvotes: 2

Anteru
Anteru

Reputation: 19404

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

Upvotes: 4

GEOCHET
GEOCHET

Reputation: 21323

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.

Upvotes: 2

Related Questions