什么是ldsfld和ldstr在IL之间有什么不同? [英] What's the different between ldsfld and ldstr in IL?
问题描述
我读过有关的String.Empty一些文章与我也做测试由我自己。不同它们之间是以下
I've read some article about String.Empty vs "" and I also do test by my self. Different between them are below.
的String.Empty
String.Empty
L_0001: ldsfld string [mscorlib]System.String::Empty
,
L_0001: ldstr ""
在我和我的朋友谈论他们认为的String.Empty比快,是因为引擎盖下(在汇编级)ldstr做多1圈比ldsfld。 (我不记得的步骤,使他们不同的)
After I talk with my friends they argue that String.Empty is faster than "" because under the hood (at assembly level) ldstr do more 1 circle than ldsfld. (I can't remember steps that make them different)
我想知道我怎么能查看性能这个方面。
I want to know how can I check about this aspect of performance.
推荐答案
在 ldsfld
操作推一个静态字段到计算堆栈的值,而 ldstr
推的引用,元数据字符串。
The ldsfld
operation pushes the value of a static field onto the evaluation stack, while the ldstr
pushes a reference to a meta data string literal.
的性能差(如果有的话)将是最小的。较新版本的编译器实际上是替补,
为的String.Empty
。
The performance difference (if any) would be minimal. Newer versions of the compiler actually substitutes ""
for String.Empty
.
您还应该考虑的code的可读性和可维护性。使用的String.Empty
它更清晰,你实际上意味着一个空字符串,而不是只是忘了字符串的东西输入。
You should also consider the readability and maintainability of the code. Using String.Empty
it's clearer that you actually mean an empty string and not just forgot to type something in the string literal.
我看了一下创建本地code。
I took a look at the native code created.
C#3,发行方式,86:
C# 3, release mode, x86:
string a = String.Empty;
0000002a mov eax,dword ptr ds:[0356102Ch]
0000002f mov dword ptr [ebp-8],eax
string b = "";
00000032 mov eax,dword ptr ds:[0356202Ch]
00000038 mov dword ptr [ebp-0Ch],eax
C#3,发行方式,64:
C# 3, release mode, x64:
string a = String.Empty;
0000003b mov rax,12711050h
00000045 mov rax,qword ptr [rax]
00000048 mov qword ptr [rsp+28h],rax
string b = "";
0000004d mov rax,12713048h
00000057 mov rax,qword ptr [rax]
0000005a mov qword ptr [rsp+30h],rax
那么,到底是code是相同的。
So, in the end the code is identical.
这篇关于什么是ldsfld和ldstr在IL之间有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!