2

I've read multiple places that in Java 1.5+ String concatenations are optimized to using a StringBuilder when a program is compiled. It's unclear to me if this is a standard or just a common optimization many compilers employ. Any clarificion in regards to this would be appreciated, but mainly it's a lead-in to my second question.

Does .NET similarly optimize? I'm aware that if I use StringBuilder this will eliminate any ambiguity but I personally find the simplicity of + easier to read. If .NET does, did this start in a specific version? Elaboration is appreciated.

vpiTriumph
  • 3,116
  • 2
  • 27
  • 39
  • That's not really an "optimization"; in fact, it's often slower than allowing mutable strings would be. – Michael Myers Jul 05 '12 at 19:29
  • 1
    [Why is String.Concat not optimized to StringBuilder.Append?](http://stackoverflow.com/questions/2177447/why-is-string-concat-not-optimized-to-stringbuilder-append) – Tim Schmelter Jul 05 '12 at 19:38
  • 1
    @MichaelMyers It's an optimization; it is faster and probably generates less garbage than creating intermediate immutable strings. Allowing mutable strings is not an optimization, as those have entirely different semantics. Unless you only want to use those under the hood for optimizing the very same cases which are amendable to the `StringBuilder` optimization. But then there is no performance advantage - even in the best case, the mutable string must do exactly as much copying as a string builder whose result is used once. –  Jul 05 '12 at 19:39
  • @TimSchmelter that article hits on the possible thinking of the .NET compiler design team, very interesting. I wonder why the Java side came to a different conclusion. – vpiTriumph Jul 05 '12 at 19:43

5 Answers5

4

From MSDN:

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is appended to the new buffer.

The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input.

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

Ant P
  • 24,820
  • 5
  • 68
  • 105
1

Hope this will give you a better view towards StringBuilder then string conctenation with +

Performance Considerations

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is appended to the new buffer.

The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input.

MSDN

HatSoft
  • 11,077
  • 3
  • 28
  • 43
0

No, AFAIK .NET doesn't do this automatically, you need to be specific. Using + creates every time a new string, because type string is immutable.

I'd suggest reading this for example: Most efficient way to concatenate strings?

Hint - accepted answer may not be always the correct one.

And this : http://en.csharp-online.net/Manipulating_Strings_in_CSharp%E2%80%94Concatenating_Strings

Community
  • 1
  • 1
walther
  • 13,466
  • 5
  • 41
  • 67
  • Contrary to the accepted answer of the question you link to, using a StringBuilder is not necessarily an optimisation of using String concatenations. – Ant P Jul 05 '12 at 19:31
  • @AntP, never said it is. I've included the link because it features some interesting discussion on the topic, which may interest the OP. – walther Jul 05 '12 at 19:33
  • You didn't, but your source does. Worth qualifying if you're going to use it. – Ant P Jul 05 '12 at 19:34
0

No, + operator isn't optimized with StringBuilder in .NET . In MSDN, there is a mention like, if you want to speed up string operation, you can use StringBuilder

Mehmet Ali Sert
  • 184
  • 2
  • 9
0

If you concatenate multiple strings in one operation with String.Concat, .net will add together their lengths, create a new string object of suitable size, and copy all of the characters from the source strings to the new one. If you are doing four or fewer strings in a String.Concat call, one new object will be created (the new string); no "garbage" will be created unless one of the old strings is abandoned and thus becomes garbage. If you concatenate five or more strings, a new temporary String[] will be created to hold the parameters, but it will be small and short-lived (and thus relatively harmless), regardless of the length of the strings, unless you're passing many thousands of parameters.

Additionally, one can concatenate any number of constant strings using the + operator in C#, or the + or & operators in vb.net and the compiler will turn them into one larger constant string. I'm not sure what rules dictate the extent to which the C# or vb.net compiler will consolidate strings in an expression which contains multiple constant and non-constant strings (especially if parentheses are involved); using a String.Concat call with as many parameters as needed, but using + to consolidate constant strings within a parameter, should yield optimal code in any case.

supercat
  • 77,689
  • 9
  • 166
  • 211