0

I know you should use a StringBuilder when concatenating strings but I was just wondering if there is a difference in concatenating string variables and string literals. So, is there a difference in performance in building s1, s2, and s3?

string foo = "foo";
string bar = "bar";

string s1 = "foo" + "bar";
string s2 = foo + "bar";
string s3 = foo + bar;
Homer
  • 7,594
  • 14
  • 69
  • 109

5 Answers5

3

In the case you present, it's actually better to use the concatenation operator on the string class. This is because it can pre-compute the lengths of the strings and allocate the buffer once and do a fast copy of the memory into the new string buffer.

And this is the general rule for concatenating strings. When you have a set number of items that you want to concatenate together (be it 2, or 2000, etc) it's better to just concatenate them all with the concatenation operator like so:

string result = s1 + s2 + ... + sn;

It should be noted in your specific case for s1:

string s1 = "foo" + "bar";

The compiler sees that it can optimize the concatenation of string literals here and transforms the above into this:

string s1 = "foobar";

Note, this is only for the concatenation of two string literals together. So if you were to do this:

string s2 = foo + "a" + bar;

Then it does nothing special (but it still makes a call to Concat and precomputes the length). However, in this case:

string s2 = foo + "a" + "nother" + bar;

The compiler will translate that into:

string s2 = foo + "another" + bar;

If the number of strings that you are concatenating is variable (as in, a loop which you don't know beforehand how many elements there are in it), then the StringBuilder is the most efficient way of concatenating those strings, as you will always have to reallcate the buffer to account for the new string entries being added (of which you don't know how many are left).

casperOne
  • 73,706
  • 19
  • 184
  • 253
2

The compiler can concatenate literals at compile time, so "foo" + "bar" get compiled to "foobar" directly, and there's no need to do anything at runtime.

Other than that, I doubt there's any significant difference.

Mattias S
  • 4,748
  • 2
  • 17
  • 18
1

There is no difference between s2 and s3. The compiler will take care of s1 for you, and concatenate it during compile time.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
1

Your "knowledge" is incorrect. You should sometimes use a StringBuilder when concatenating strings. In particular, you should do it when you can't perform the concatenation all in one experession.

In this case, the code is compiled as:

string foo = "foo";
string bar = "bar";

string s1 = "foobar";
string s2 = String.Concat(foo, "bar");
string s3 = String.Concat(foo, bar);

Using a StringBuilder would make any of this less efficient - in particular it would push the concatenation for s1 from compile time to execution time. For s2 and s3 it would force the creation of an extra object (the StringBuilder) as well as probably allocating a string which is unnecessarily large.

I have an article which goes into more detail on this.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Also String.Join is useful (when you need a delimiter between strings) and has the same performance of string.Concat. – munissor Apr 09 '10 at 14:07
  • I came across a performance-related question wherein the decision to use a StringBuilder versus concatenation operators seemed to relate primarily to how many strings were being concatenated. For less than six, the operator was optimal. For six or more, the StringBuilder was optimal. Though I am sure mileage varies. – JYelton Apr 09 '10 at 15:19
  • @JYelton: It will very much depend on the lengths of the strings, how many reallocations are necessary etc. – Jon Skeet Apr 09 '10 at 15:22
  • Thanks, that of course makes more sense than some arbitrary number of concatenations. – JYelton Apr 09 '10 at 15:52
-1

I'd say that this should decide compiler. Because all your string-building can be optimized as values is already known.
I guess StringBuilder pre-allocates space for appending more strings. As You know + is binary operator so there is no way to build concatenation of more than two strings at a time. Thus if you want to do s4 = s1 + s2 + s3 it will require building intermediate string (s1+s2) and only after that s4.

ony
  • 12,457
  • 1
  • 33
  • 41
  • Not true. "s4 = s1 + s2 + s3" will be compiled as "s4 = string.Concat(s1, s2, s3);". Try it. – Jon Skeet Apr 09 '10 at 14:01
  • I think some people got the idea. For `foo+bar` there is no intermediate string, but for `s1 + .. + sn` where `n` is maximum arity of that `string.Concat` + 1 will require some intermediate data. – ony Apr 09 '10 at 14:08
  • 1
    string.Concat has a method which takes an array of objects. The compiler will use that where necessary. – Jon Skeet Apr 09 '10 at 14:12
  • @Jon that array will be the intermediate object ;) – ony Apr 09 '10 at 17:07