Lar*_*nal 46 .net c# optimization performance
例如,编译器是否知道要翻译
string s = "test " + "this " + "function";
Run Code Online (Sandbox Code Playgroud)
至
string s = "test this function";
Run Code Online (Sandbox Code Playgroud)
从而避免字符串连接的性能损失?
Mic*_*urr 21
只是关于相关主题的附注 - C#编译器还将使用' +'运算符'优化'涉及非文字的多个连接,以单次调用String.Concat()方法的多参数重载.
所以
string result = x + y + z;
Run Code Online (Sandbox Code Playgroud)
编译成等价的东西
string result = String.Concat( x, y, z);
Run Code Online (Sandbox Code Playgroud)
而不是更天真的可能性:
string result = String.Concat( String.Concat( x, y), z);
Run Code Online (Sandbox Code Playgroud)
没有什么是惊天动地的,但只想将这一点添加到关于字符串文字级联优化的讨论中.我不知道这种行为是否是语言标准规定的.
是.
C#不仅优化了字符串文字的连接,还将等效的字符串文字折叠为常量,并使用指针引用对同一常量的所有引用.
从马嘴里说:
连接是将一个字符串附加到另一个字符串末尾的过程。当您使用 + 运算符连接字符串文字或字符串常量时,编译器会创建单个字符串。不会发生运行时串联。但是,字符串变量只能在运行时连接。在这种情况下,您应该了解各种方法对性能的影响。
http://msdn.microsoft.com/en-us/library/ms228504.aspx
是的 - 您可以使用ILDASM显式地看到这一点.
例:
这是一个类似于您的示例后跟编译的CIL代码的程序:
注意:我正在使用String.Concat()函数来查看编译器如何处理两种不同的连接方法.
程序
class Program
{
static void Main(string[] args)
{
string s = "test " + "this " + "function";
string ss = String.Concat("test", "this", "function");
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
注意编译器在IL_0001创建常量"测试此函数"而不是编译器如何处理String.Concat()函数 - 它为每个.Concat()参数创建一个常量,然后调用.Concat()功能.
| 归档时间: |
|
| 查看次数: |
5800 次 |
| 最近记录: |