简体   繁体   English

C#是否优化字符串文字的串联?

[英]Does C# optimize the concatenation of string literals?

For instance, does the compiler know to translate 例如,编译器是否知道要翻译

string s = "test " + "this " + "function";

to

string s = "test this function";

and thus avoid the performance hit with the string concatenation? 从而避免使用字符串连接对性能造成影响?

Yes. 是。 This is guaranteed by the C# specification. 这由C#规范保证。 It's in section 7.18 (of the C# 3.0 spec): 在C#3.0规范的7.18节中:

Whenever an expression fulfills the requirements listed above, the expression is evaluated at compile-time. 只要表达式满足上面列出的要求,就会在编译时对表达式求值。 This is true even if the expression is a sub-expression of a larger expression that contains non-constant constructs. 即使表达式是包含非恒定构造的较大表达式的子表达式,也是如此。

(The "requirements listed above" include the + operator applied to two constant expressions.) (“上面列出的要求”包括应用于两个常量表达式的+运算符。)

See also this question . 另请参阅此问题

Just a side note on a related subject - the C# compiler will also 'optimize' multiple concatenations involving non-literals using the ' + ' operator to a single call to a multi-parameter overload of the String.Concat() method. 只是一个相关主题的旁注-C#编译器还将使用' + '运算符对String.Concat()方法的多参数重载的单个调用来“优化”涉及非文字的多个串联。

So 所以

string result = x + y + z;

compiles to something equivalent to 编译成相当于

string result = String.Concat( x, y, z);

rather than the more naive possibility: 而不是更幼稚的可能性:

string result = String.Concat( String.Concat( x, y), z);

Nothing earth-shattering, but just wanted to add this bit to the discussion about string literal concatenation optimization. 没什么大不了的,但只是想在有关字符串文字串联优化的讨论中添加这一点。 I don't know whether this behavior is mandated by the language standard or not. 我不知道这种行为是否是语言标准规定的。

Yes. 是。

C# not only optimizes the concatenation of string literals, it also collapses equivalent string literals into constants and uses pointers to reference all references to the same constant. C#不但可以优化字符串文字的串联,还可以将等效的字符串文字折叠成常量,并使用指针引用所有对同一常量的引用。

Yes - You can see this explicitly using ILDASM. 是的-您可以使用ILDASM明确看到这一点。

Example: 例:

Here's a program that is similar to your example followed by the compiled CIL code: 这是一个与您的示例相似的程序,后跟已编译的CIL代码:

Note: I am using the String.Concat() function just to see how the compiler treats the two different methods of concatenation. 注意:我使用String.Concat()函数只是为了查看编译器如何处理两种不同的串联方法。

Program 程序

class Program
{
    static void Main(string[] args)
    {
        string s = "test " + "this " + "function";
        string ss = String.Concat("test", "this", "function");
    }
}

ILDASM 伊尔达斯

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       29 (0x1d)
  .maxstack  3
  .locals init (string V_0,
           string V_1)
  IL_0000:  nop
  IL_0001:  ldstr      "test this function"
  IL_0006:  stloc.0
  IL_0007:  ldstr      "test"
  IL_000c:  ldstr      "this"
  IL_0011:  ldstr      "function"
  IL_0016:  call       string [mscorlib]System.String::Concat(string,
                                                              string,
                                                              string)
  IL_001b:  stloc.1
  IL_001c:  ret
} // end of method Program::Main

Notice how at IL_0001 the compiler created the constant "test this function" as opposed to how the compiler treats the String.Concat() function - which creates a constant for each of the .Concat() params, then calls the .Concat() function. 请注意,在IL_0001上,编译器如何创建常量“测试此函数”,而不是如何对待String.Concat()函数-后者为每个.Concat()参数创建一个常量,然后调用.Concat()功能。

From the horses mouth: 从马口看:

Concatenation is the process of appending one string to the end of another string. 串联是将一个字符串附加到另一字符串末尾的过程。 When you concatenate string literals or string constants by using the + operator, the compiler creates a single string. 当使用+运算符连接字符串文字或字符串常量时,编译器将创建单个字符串。 No run time concatenation occurs. 没有运行时级联发生。 However, string variables can be concatenated only at run time. 但是,字符串变量只能在运行时连接。 In this case, you should understand the performance implications of the various approaches. 在这种情况下,您应该了解各种方法对性能的影响。

http://msdn.microsoft.com/en-us/library/ms228504.aspx http://msdn.microsoft.com/en-us/library/ms228504.aspx

我相信答案是肯定的,但是您必须查看编译器吐出的内容……只是进行编译,并在其上使用反射器:-)

I had a similar question, but about VB.NET instead of C#. 我有一个类似的问题,但是关于VB.NET而不是C#。 The simplest way of verifying this was to view the compiled assembly under Reflector. 验证这一点的最简单方法是在Reflector下查看已编译的程序集。

The answer was that both the C# and VB.NET compiler optimise concatenation of string literals. 答案是C#和VB.NET编译器都优化了字符串文字的串联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM