Which is efficient? "" or string.Empty ?
-
Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.
-
Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.
Hi! Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". Regards, mav
-
Hi! Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". Regards, mav
-
Hi! Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". Regards, mav
mav.northwind wrote: Using "" will be marginally faster because the runtime doesn't have to look up a constant, but I strongly doubt that you will notice a difference in real life. If you look at the difference in the IL, string.Empty almost sounds faster.
ldstr ""
vs.
ldsfld string [mscorlib]System.String::Empty
With
ldstr
, it's pushing an object reference to a string literal stored in the metabase onto the stack where asldsfld
pushes the value of a static field onto the stack. mav.northwind wrote: But I think it's more important to keep an eye on good readability than to try and tune execution speed by replacing string.Empty with "". I completely agree.- Nick Parker Microsoft MVP - Visual C#
My Blog | My Articles -
But both string.Empty and "" will be constants. The only difference is where they are declared. The constant for string.Empty already exists, so there are hardly any point of creating more constants that are identical. --- b { font-weight: normal; }
Not entirely correct, I think. I looked it up in IL. Using the constant
string.Empty
compiles toldsfld string [mscorlib]System.String::Empty
whereas using""
yieldsldstr ""
So I'd assume the runtime would have to check at least once whether mscorlib is loaded already (which is, of course) to be able to access a constant defined in a type from this assembly. Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, but, as I wrote, I don't think you'll be able to notice it. Regards, mav -
Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.
On a related note, if you're checking to see whether a string is empty, it is more efficient to use if(str.Length == 0) instead of if(str == "") or if(str == string.Empty)
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Cops & Robbers Judah Himango
-
Not entirely correct, I think. I looked it up in IL. Using the constant
string.Empty
compiles toldsfld string [mscorlib]System.String::Empty
whereas using""
yieldsldstr ""
So I'd assume the runtime would have to check at least once whether mscorlib is loaded already (which is, of course) to be able to access a constant defined in a type from this assembly. Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, but, as I wrote, I don't think you'll be able to notice it. Regards, mavmav.northwind wrote: Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, Here is the optimized JIT code. Same thing... except with "" you will be adding more strings to the string table of the assembly.
16: string a = "";
0000000f mov eax,dword ptr ds:[01AA1010h]
00000015 mov esi,eax
17: string b = string.Empty;
00000017 mov eax,dword ptr ds:[01AA2014h]
0000001c mov edi,eaxxacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
mav.northwind wrote: Using "" doesn't require this lookup, so it should be _a wee little bit_ faster, Here is the optimized JIT code. Same thing... except with "" you will be adding more strings to the string table of the assembly.
16: string a = "";
0000000f mov eax,dword ptr ds:[01AA1010h]
00000015 mov esi,eax
17: string b = string.Empty;
00000017 mov eax,dword ptr ds:[01AA2014h]
0000001c mov edi,eaxxacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
I expected it to be very little difference in the compiled code, but I didn't expect it to be as good as no difference at all. :) Anyway, regardless of how it is currently implemented, the code that best resembles your intention will often be the most efficient. The compiler will be able to optimize the code better if the information you give it is better. In some future implementation or on a different system, using string.Empty might even be the most efficient way. --- b { font-weight: normal; }
-
Hi, Please clarify which is more efficient. using string.Empty or "". If so please explain why is this. Iam often getting confused on this. Thanks.
May be this helpString.Empty vs "" [^] MCAD