tags:

views:

45

answers:

2

I've read some article about String.Empty vs "" and I also do test by my self. Different between them are below.

String.Empty

L_0001: ldsfld string [mscorlib]System.String::Empty

""

L_0001: ldstr ""

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.

+3  A: 

ldstr is IL to load a specific string token from metadata.

ldsfld is IL to load the specified field - which in this case is string.Empty.

In other words, they're entirely different operations, which happen to have the same result in this case. How are they implemented at the assembly level? Well, that could very well depend on the version of the CLR you're using. Ask your friends which version they're talking about... desktop (32 or 64 bit? 1, 2, 2SP1, 2SP2, 4?), Compact Framework (again, which version?), Silverlight (which operating system, which version?) Did they use cordbg on the code you're actually discussing, or did they do it on some sample code, which may not have been optimized in the same way?

I would (and have) argued that you should use whichever you find more readable. Personally I prefer "" but others prefer string.Empty. That's fine. Arguing for one over the other on performance reasons requires evidence though... and ideally, evidence based on the code you're actually writing, not a micro-benchmark.

I would be astonished to see code where any difference between the two actually led to a significant performance difference in real code - other than in situations where there's probably a better way of approaching the task anyway.

Jon Skeet
+3  A: 

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.

The performance difference (if any) would be minimal. Newer versions of the compiler actually substitutes "" for 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.

Edit:

I took a look at the native code created.

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, 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 

So, in the end the code is identical.

Guffa
+1 for looking at assembly, because this is what actually makes the difference.
Steven