Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addition of strings in c#, how the compiler does it?

A = string.Concat("abc","def") 

B = "abc" + "def"

A vs. B

Lately I have been confused why many would say that definitely A does a much faster processing compared to B. But, the thing is they would just say because somebody said so or because it is just the way it is. I suppose I can hear a much better explaination from here.

How does the compiler treats these strings?

Thank you!

like image 449
Carls Jr. Avatar asked Jan 21 '11 10:01

Carls Jr.


People also ask

Can we add two strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

Does C have concatenation?

As you know, the best way to concatenate two strings in C programming is by using the strcat() function.

What is string concatenation function in C?

(String Concatenation) In the C Programming Language, the strcat function appends a copy of the string pointed to by s2 to the end of the string pointed to by s1. It returns a pointer to s1 where the resulting concatenated string resides.


3 Answers

The very first thing I did when I joined the C# compiler team was I rewrote the optimizer for string concatenations. Good times.

As already noted, string concats of constant strings are done at compile time. Non-constant strings do some fancy stuff:

a + b --> String.Concat(a, b)
a + b + c --> String.Concat(a, b, c)
a + b + c + d --> String.Concat(a, b, c, d)
a + b + c + d + e --> String.Concat(new String[] { a, b, c, d, e })

The benefits of these optimizations are that the String.Concat method can look at all the arguments, determine the sum of their lengths, and then make one big string that can hold all the results.

Here's an interesting one. Suppose you have a method M that returns a string:

s = M() + "";

If M() returns null then the result is the empty string. (null + empty is empty.) If M does not return null then the result is unchanged by the concatenation of the empty string. Therefore, this is actually optimized as not a call to String.Concat at all! It becomes

s = M() ?? ""

Neat, eh?

like image 137
Eric Lippert Avatar answered Sep 24 '22 08:09

Eric Lippert


Read this: The Sad Tragedy of Micro-Optimization Theater (Coding Horror)

like image 42
bniwredyc Avatar answered Sep 23 '22 08:09

bniwredyc


In C#, the addition operator for strings is just syntactic sugar for String.Concat. You can verify that by opening the output assembly in reflector.

Another thing to note is, if you have string literals (or constants) in your code, such as in the example, the compiler even changes this to B = "abcdef".

But, if you use String.Concat with two string literals or constants, String.Concat will still be called, skipping the optimization, and so the + operation would actually be faster.

So, to sum it up:

stringA + stringB becomes String.Concat(stringA, stringB).
"abc" + "def" becomes "abcdef"
String.Concat("abc", "def") stays the same

Something else i just had to try:

In C++/CLI, "abc" + "def" + "ghi" is actually translated to String.Concat(String.Concat("abc", "def"), "ghi")

like image 22
Botz3000 Avatar answered Sep 24 '22 08:09

Botz3000