Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation vs String Builder. Performance

I have a situation where I need to concatenate several string to form an id of a class. Basically I'm just looping in a list to get the ToString values of the objects and then concatenating them.

foreach (MyObject o in myList)   result += o.ToString(); 

The list is NOT expected to have more than 5 elements (although it could but that's a very, very marginal case) and usually will have from 1 to 3 elements, being common for it to just have one or two.

What would be more performance, keeping the concatenation or using an StringBuilder?

StringBuilder bld = new StringBuilder() foreach (MyObject o in myList)   bld.Append(o.ToString()); 

I'm unsure if creating the StringBuilder will take more time than standard concatenation for the most usual case.

This is lazy, items on the list do not change once created so the id is lazily constructed once when called.

As a side note... Should I use a fixed array instead of a List? Would I get any performance or memory improvement if I do? (List is only used as an IEnumerable anyway)

A more general view of the question could be, how many strings are enough to stop concatenating and start building?

Should I even bother to test case the scenario?

if (myList.Count > 4)    ConcatWithStringBuilder(myList); 
like image 860
Jorge Córdoba Avatar asked Oct 23 '09 11:10

Jorge Córdoba


People also ask

Which is faster string concatenation or the StringBuilder class?

Note that regular string concatenations are faster than using the StringBuilder but only when you're using a few of them at a time. If you are using two or three string concatenations, use a string.

Is string concatenation slow?

Each time strcat calls, the loop will run from start to finish; the longer the string, the longer the loop runs. Until the string is extensive, the string addition takes place very heavy and slow.

Which is more efficient string or StringBuilder?

Equals is more efficient, it will only check if the strings match. Contains will search the for that text inside the string.

Is string builder more efficient?

StringBuilder is efficient in the first example because it acts as a container for the intermediate result without having to copy that result each time - when there's no intermediate result anyway, it has no advantage.


1 Answers

The usual answer is that string concatenation is more efficient for between 4 to 8 strings. It depends on whose blog you read.

Don't write a test to decide on which method to use. If you are unsure of whether it will go over the magic limit, then just use StringBuilder.

Run this code to see the results for yourself:

const int sLen=30, Loops=5000; DateTime sTime, eTime; int i; string sSource = new String('X', sLen); string sDest = ""; //  // Time string concatenation. //  sTime = DateTime.Now; for(i=0;i<Loops;i++) sDest += sSource; eTime = DateTime.Now; Console.WriteLine("Concatenation took " + (eTime - sTime).TotalSeconds + " seconds."); //  // Time StringBuilder. //  sTime = DateTime.Now; System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1)); for(i=0;i<Loops;i++) sb.Append(sSource); sDest = sb.ToString(); eTime = DateTime.Now; Console.WriteLine("String Builder took " + (eTime - sTime).TotalSeconds + " seconds."); //  // Make the console window stay open // so that you can see the results when running from the IDE. //  Console.WriteLine(); Console.Write("Press Enter to finish ... "); Console.Read(); 

Ref. http://support.microsoft.com/kb/306822

like image 130
Daniel Robinson Avatar answered Sep 21 '22 16:09

Daniel Robinson