2

I was taught that to improve performance in VB.NET, we can use StringBuilder to concatenate strings.

StringBuilder1.Append(string1)
StringBuilder1.Append(string2)

As opposed to:

string1 = string1 & string2

Likewise, is there a performance gain of StringBuilder over &= concatenation?

StringBuilder1.Append(string1)
StringBuilder1.Append(string2)

As opposed to:

string1 &= string2

A more senior developer on my team is opposed to the use of StringBuilder. Is there a way to benchmark these differences? Or is the difference insignificant except in heavy volume environments? I know there are lots of answers to StringBuilder vs. concatenation on the web and SO, but I couldn't find any specifically related to &= concatenation.

mrcoulson
  • 1,331
  • 6
  • 20
  • 35
  • 2
    You won't much benefit til you are appending many more line. Simple concatenation of 2 string is not going to gain you any more performance. – OneFineDay Dec 17 '15 at 21:02
  • 1
    This may be helpful: http://www.dotnetperls.com/stringbuilder-performance – David Tansey Dec 17 '15 at 21:02
  • 2
    Your senior devoloper is right. StringBuilder should be used to build strings probably when a loop is involved, not just to concatenate a couple of strings. Using &= doesn't make any difference here. – Steve Dec 17 '15 at 21:08
  • ...OR most cases where you are assembling a string from bits of other strings (which typically involves a loop) – Ňɏssa Pøngjǣrdenlarp Dec 17 '15 at 21:18
  • Okay cool. The useage we were debating is actually in a loop, so I feel okay about having used StringBuilder there. – mrcoulson Dec 17 '15 at 21:28

2 Answers2

1

a &= b is just a shortcut for a = a & b. Therefore their speeds are equal.

StringBuilder is not faster in concatenating 2 strings, however if you are appending strings repeatedly, it can work more efficiently, because it uses a clever algorithm in order to reduce memory allocations and string copy operations.

On the other hand, a string concatenation always creates a new string and therefore allocates new memory each time and copies the successively growing string each time.

a = "hello"
a &= b
a &= c
a &= d
...

This creates and allocates memory for new strings each time.

According to Eric Lippert "In the new implementation, the string builder maintains a linked list of relatively small arrays, and appends a new array onto the end of the list when the old one gets full".

The final string is then allocated only once (since now its final length is known) and all the string snippets are copied to it.

Memory allocations are fast. The real problem comes when the garbage collector wants to collect unused memory. The more memory chunks have to be evaluated for collection, the more time it takes and this process is much more time consuming than the allocation of memory.

Community
  • 1
  • 1
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Excellent. Lots of great answers in the comments also. Wish I could accept them all as answers. Thanks! – mrcoulson Dec 17 '15 at 21:27
  • 2
    The allocation and collection does have some cost, but there is also a potentially very large cost in copying the strings. When naively concatenating "A", "B", "C", "D", "E", we copy "A" and "B" to "AB", then we copy "AB" and "C" to "ABC", and so on. If you work it out, to concatenate n strings this way you end up moving n-squared bytes of memory. If n is 10, no worries. If n is a million, you have a serious problem. – Eric Lippert Dec 18 '15 at 02:28
  • Yes, Jon Skeet explains this in detail with simple examples and time measurements in this article: [Concatenating Strings Efficiently](http://www.yoda.arachsys.com/csharp/stringbuilder.html). – Olivier Jacot-Descombes Dec 18 '15 at 12:54
0

String is immutable. That means that for each string operation, new memory allocation occurs. That is not good in terms of performance and resources. StringBuilder works little better with those BUT it comes with the cost of instantiation the StringBuilder.

My way of working with string.

So if I have to concatenate 3-4 strings, I do it with the standard way. In loops (especially when I expect a lot of operations ) I always use StringBuilder.

shadow
  • 1,883
  • 1
  • 16
  • 24