More on String.Empty


href="http://www.webjives.com">Deepak Sharma style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> style="mso-spacerun: yes">  href="http://blogs.gotdotnet.com/BradA/commentview.aspx/77de1e94-b77d-4f93-9bb5-87d09bb12323">asks: prefix = o ns = "urn:schemas-microsoft-com:office:office"
/>


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> style="mso-tab-count: 1">           
style="mso-bidi-font-style: normal">Where does String.Length == 0 stands in
terms of memory efficiency compared to these two methods? Any
ideas.


class=commentbody> style="FONT-SIZE: 10pt; TEXT-TRANSFORM: uppercase; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">I chatted with the Dev lead that
owns String… here is his response:


style="mso-bidi-font-style: normal"> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="mso-bidi-font-style: normal"> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Actually, there is no memory effect
here. There is always an empty string available. However, the code path for
String.Length == 0 is significantly shorter.


style="mso-bidi-font-style: normal"> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


"urn:schemas:contacts" /> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Hope style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> that helps!


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


 

Comments (4)

  1. Brian Grunkemeyer says:

    I posted this elsewhere, but just to make sure this gets seen:

    There are two reasons why looking at the length of the string is faster.

    1) Accessing static variables in appdomain-neutral assemblies in ngen’ed images involves going through one more level of indirection. So you dereference an extra pointer. (This may be fixed in a future version like v2.0 – you’d have to talk to one of our two ngen experts.)

    2) Since we have overloaded operator == on String, this is a method call. Calling Object.ReferenceEquals(x, String.Empty) would be a bit faster, but still suffers from #1.

    Yes, a language compiler or our JIT could theoretically optimize this. I don’t know of any that do. It seems like a bit of a corner case. But feel free to talk to your compiler vendors about this.