String concatenation vs StringBuilder

  Got curious at which point StringBuilder actually becomes faster than plain string concatenation, so wrote a little test.

The results depend on a string length, but with a string of 30 characters the break-even point happens at about 10 concatenations. If there’s less than 10 concatenations – “abc” + “def” is faster. The longer the string – the faster StringBuilder gets ahead.

Would be interesting to plot a graph for different string lengths and number of concatenations.

 EDIT:

here it is. Tested strings 20 and 100 chars long, in a 10000 times loop. X axis is number of concatenations, Y axis is milliseconds.

Pretty much what should be expected: the string concatenation times skyrocket as the number of concatenations increases, especially with long strings.

StringBuilder stays pretty flat.

 

Leave a Reply

Your email address will not be published. Required fields are marked *