Why isn't string concatenation automatically converted to StringBuilder in C#? [duplicate]

穿精又带淫゛_ 提交于 2019-12-12 09:36:14

问题


Possible Duplicate:
Why is String.Concat not optimized to StringBuilder.Append?

One day I was ranting about a particular Telerik control to a friend of mine. I told him that it took several seconds to generate a controls tree, and after profiling I found out that it is using a string concatenation in a loop instead of a StringBuilder. After rewriting it worked almost instantaneously.

So my friend heard that and seemed to be surprised that the C# compiler didn't do that conversion automatically like the Java compiler does. Reading many of Eric Lippert's answers I realize that this feature didn't make it because it wasn't deemed worthy enough. But if, hypothetically, costs were small to implement it, what rationale would stop one from doing it?


回答1:


I note that this is an exact duplicate of

Why is String.Concat not optimized to StringBuilder.Append?

so this should probably be closed.

But if, hypothetically, costs were small to implement it, what rationale would stop one from doing it?

It sounds like you're proposing a bit of a tautology: if there is no reason to not do X, then is there a reason to not do X? No.

I see little value in knowing the answers to hypothetical, counterfactual questions. Perhaps a better question to ask would be a question about the real world:

Are there programming languages that use this optimization?

Yes. In JScript.NET, we detect string concatenations in loops and the compiler turns them into calls to a string builder.

That might then be followed up with:

What are some of the differences between JScript .NET and C# that justify the optimization in the one language but not in the other?

A core assumption of JScript.NET is that its programmers are mostly going to be JavaScript programmers, and many of them will have already built libraries that must run in any implementation of ECMAScript. Those programmers might not know the .NET framework well, and even if they do, they might not be able to use StringBuilder without making their library code non-portable. It is also reasonable to assume that JavaScript programmers may be either novice programmers, or programmers who came to programming via their line of business rather than a course of study in computer science.

C# programmers are far more likely to know the .NET framework well, to write libraries that work with the framework, and to be experienced programmers who understand why looped string concatenation is O(n2) in the naive implementation. They need this optimization generated by the compiler less because they can just do it themselves if they deem it necessary.

In short: compiler features are about spending our budget to add value for the customer; you get more "bang for buck" adding the feature to JScript.NET than you do adding it to C#.




回答2:


The C# compiler does better than that.

a + b + c is compiled to String.Concat(a, b, c), which is faster than StringBuilder.
"a" + "b" is compiled directly to "ab" (useful for multi-line literals).

The only place to use StringBuilder is when concatenating repetitively inside a loop; the compiler cannot easily optimize that.



来源:https://stackoverflow.com/questions/9124203/why-isnt-string-concatenation-automatically-converted-to-stringbuilder-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!