tag:blogger.com,1999:blog-5430866.post2554203238662499875..comments2024-01-02T08:54:14.406-05:00Comments on Peter Bromberg's UnBlog: String.Empty vs “” vs String. IsNullOrEmptypeterbromberghttp://www.blogger.com/profile/18173639411723574123noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-5430866.post-84716002830963495942010-03-08T14:04:51.357-05:002010-03-08T14:04:51.357-05:00public void Test()
{
string str1 = ""; /...public void Test()<br />{<br />string str1 = ""; // this Does create a new object<br />for (int i=0; i < 1000; i++)<br /> {<br /> string str2 = ""; // these do not - they refer to the interned string str1<br /> }<br />}<br />.On invoking Test only one memory heap allocation takes place for the empty string that is only at <br />string str1 = "";peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-61854560004482605832010-03-08T12:00:40.806-05:002010-03-08T12:00:40.806-05:00вот как надо с умным видом заниматся хренью, ага
...вот как надо с умным видом заниматся хренью, ага<br /><br />Actually, myString = “” does not create any new instances. <br />var a = "sometext";<br />var b = "sometext";<br />reference a will be equal to reference b, not just because of overrided operator==. csc joins equal string constans.<br /> A good code optimizer should even understand that myString does not change through the loop and ommit multiple comparation with constant. But csc does not.<br /><br />By the way, String.IsNullOrEmpty can be inlined during jit'ting. Hence the results can be differnet without debugger, with release build and with optimization.<br /><br />Anyway, this kind of optimization won't ever change anything.<br /><br />Cheers, <br />Andrey.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-6840138532405880312010-03-04T16:53:02.623-05:002010-03-04T16:53:02.623-05:00@Jeremy,
We were actually discussing this very the...@Jeremy,<br />We were actually discussing this very theory on Twitter. I'm too lazy to decompile and examine the IL :-)peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-33625563332224167852010-03-04T16:13:38.452-05:002010-03-04T16:13:38.452-05:00I'd have to check, but I suspect (and would lo...I'd have to check, but I suspect (and would love for it to be true) that IsNullOrEmpty calls fit the JITter's rules for inlining, which would certainly account for it being just as fast as the explicit null check variations options I mentioned earlier. Oh, the joys of micro-optimization. :)Jeremy Graynoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-53804100882747641522010-03-04T15:22:25.238-05:002010-03-04T15:22:25.238-05:00Try 10 million or even 100 million iterations.Try 10 million or even 100 million iterations.peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-79518669879654227162010-03-04T15:19:36.865-05:002010-03-04T15:19:36.865-05:00Actually I tried this on several machines and just...Actually I tried this on several machines and just couldn't verify these results. <br /><br />I called the method several times (10000+) and wrote the results into a table. No matter what I do, IsNullOrEmpty needs fewest ticks...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-66958568904288464792010-03-04T10:17:03.593-05:002010-03-04T10:17:03.593-05:00In code like this performance is almost never a bo...In code like this performance is almost never a bottleneck.<br />Almost always primary concern in such code should be code readability (shorter version is better) and maintainability (IsNullOrEmpty is usually better).Dennis Gorelikhttps://www.blogger.com/profile/17700219093521377626noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-33093865097663930122010-03-03T16:46:56.732-05:002010-03-03T16:46:56.732-05:00@Cory,
Thanks - Already did that - its in one of t...@Cory,<br />Thanks - Already did that - its in one of the comments above yours!peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-66530087678460020382010-03-03T16:38:39.886-05:002010-03-03T16:38:39.886-05:00The code from string.IsNullOrEmpty as viewed from ...The code from string.IsNullOrEmpty as viewed from Reflector.<br /><br />public static bool IsNullOrEmpty(string value)<br />{<br /> if (value != null)<br /> {<br /> return (value.Length == 0);<br /> }<br /> return true;<br />}Anonymoushttps://www.blogger.com/profile/17886066634655585432noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-76518494252366736922010-03-03T13:06:01.667-05:002010-03-03T13:06:01.667-05:00@Jeremy,
bool isEmpty = (myString == null || mySt...@Jeremy,<br /> bool isEmpty = (myString == null || myString.Length == 0);<br /><br />seems to run faster than <br /> bool isEmpty = myString.Length == 0);<br /><br />Interestingly, the decompiled signature of String.IsNullOrEmpty looks like this:<br /><br />public static bool IsNullOrEmpty(string value)<br />{<br /> if (value != null)<br /> {<br /> return (value.Length == 0);<br /> }<br /> return true;<br />}peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1492419346652409752010-03-03T12:29:12.133-05:002010-03-03T12:29:12.133-05:00It would be interesting to see the results of anot...It would be interesting to see the results of another test run where the comparison against an empty string, whether from "" or String.Empty, and the comparison against the string's length were both preceded by a comparison of the string being null. eg:<br /><br />bool isEmpty = myString == null || myString.Length == 0;<br /><br />That would put the benchmarks on the same functional level with the one for String.IsNullOrEmpty.Jeremy Graynoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-78084516028925017962010-03-03T08:53:25.395-05:002010-03-03T08:53:25.395-05:00Oddly enough, on my system IsNullOrEmpty often tak...Oddly enough, on my system IsNullOrEmpty often takes less time to complete than comparing to "", no mather what order I run the tests in. Also, comparing to String.Empty seems to be a bit faster than to "". <br /><br />I just prefer using IsNullOrEmpty because it offers additional security against null values, where otherwise your code would fail (test n°2 in this example).Rianhttps://www.blogger.com/profile/12897681401328879691noreply@blogger.com