<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx</link><description>Sometime back I had blogged about using reference comparison to speed up ordinal string comparisons. Essentially this means that if you know for sure that your string is interned and you want to do an ordinal string comparison you can use the following</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#510245</link><pubDate>Sat, 07 Jan 2006 00:39:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510245</guid><dc:creator>Ayende Rahien</dc:creator><description>Honestly, why do you _care_ for this?&lt;br&gt;I run a _billion_ string comparision in 1.5 seconds.&lt;br&gt;&lt;br&gt;This performance difference is so small that I don't even think about it until I start doing profiling, and _that_ optimization is _way_ down the list.</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#510298</link><pubDate>Sat, 07 Jan 2006 03:30:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510298</guid><dc:creator>Frank</dc:creator><description>what about the switch statement? or has that just the same performance as the else if?&lt;br&gt;Ayende has a point, but well, its always nice to know :-)&lt;br&gt;</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#510354</link><pubDate>Sat, 07 Jan 2006 10:03:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510354</guid><dc:creator>abhinaba</dc:creator><description>Frank take a look into &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/abhinaba/archive/2005/10/28/486173.aspx"&gt;http://blogs.msdn.com/abhinaba/archive/2005/10/28/486173.aspx&lt;/a&gt;. In this blog I had posted about common string comparisons and how they work including switch. in Switch the overloded == operator is called so the final comparison occurs in String.Equal(string, string)&lt;br&gt;&lt;br&gt;As you said its nice to know. In case your application does lot of string comparison the info might be helpful... Anyways for framework development its always nice to know where time is being taken in your API... But as usual optimize only based on profiling and only when the data shows you are not meeting your perf criteria. For all other purpose elegance and maintainability wins over optimization</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#510380</link><pubDate>Sat, 07 Jan 2006 12:33:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510380</guid><dc:creator>kfarmer</dc:creator><description>Does the framework's shortcut still require the strings be interned, then?</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#510386</link><pubDate>Sat, 07 Jan 2006 13:46:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510386</guid><dc:creator>abhinaba</dc:creator><description>The reference match succeeds only for interned string. The point is I had said in the earlier post that to take the benefit of interned string explicitly do a Object reference match. With the new info I got you are equaly good if you do a string.Equals(a,b) kind of comparison. However, both strings have to be interned....</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#510410</link><pubDate>Sat, 07 Jan 2006 18:23:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510410</guid><dc:creator>Magnus Hiie</dc:creator><description>I think the ReferenceEquals method is still faster if the strings are interned because when the strings are _not_ equal, the == operator and string.Equals still perform string comparision, however ReferenceEquals can just return false.</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#510426</link><pubDate>Sat, 07 Jan 2006 20:12:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:510426</guid><dc:creator>TAG</dc:creator><description>BTW,   Why interning requere you to change reference value in your code ? &lt;br&gt;Why Runtime unable to change reference for you automaticaly ? It's allowed to move pointers all over heap as long as they are not pined. &lt;br&gt;&lt;br&gt;I.e. my suggestion is to make   string.Intern() returning void. &lt;br&gt;This can be smart move - once string.Equals  operator (or ==) find two exact strings but at different locations - then reference to one of them will be changed to another; second unreferenced and become eligible for garbage collection (even more - all others references to second string will be noticed during GC and changed).</description></item><item><title>String.Equals Performance Comparison</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#3605538</link><pubDate>Fri, 29 Jun 2007 18:54:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3605538</guid><dc:creator>Noah Coad</dc:creator><description>&lt;p&gt;I recently had an app that I used the VSTS Profiler on to find that a significant portion of time was&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#7313128</link><pubDate>Wed, 30 Jan 2008 00:35:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7313128</guid><dc:creator>Elz</dc:creator><description>&lt;p&gt;Well, i made some tests for 2 strings:&lt;/p&gt;
&lt;p&gt;string a = &amp;quot;ASDFGHJKL&amp;quot;;&lt;/p&gt;
&lt;p&gt;string b = &amp;quot;ASDFGHJ&amp;quot;; &lt;/p&gt;
&lt;p&gt;In my measurements a.Equals(b)(average: 864 ms) performed a lot faster than string.Equals(a,b) (average: 2395).&lt;/p&gt;
&lt;p&gt;I used Framework 2.0.&lt;/p&gt;
&lt;p&gt;So if you could enlighten me i would appreciate.&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#7318796</link><pubDate>Wed, 30 Jan 2008 06:42:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7318796</guid><dc:creator>abhinaba</dc:creator><description>&lt;p&gt;Do the same in a loop for say 100,000 times and then take the average....&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#7334114</link><pubDate>Thu, 31 Jan 2008 00:02:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7334114</guid><dc:creator>Elz</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;Here is my code:&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;using System.Diagnostics;&lt;/p&gt;
&lt;p&gt;namespace ConsoleApplication3&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;class Program&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string a = &amp;quot;ASDFGHJKL&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string b = &amp;quot;ASDFGHJ&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stopwatch stopper = new Stopwatch();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;stopper.Start();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (long i = 0; i &amp;lt; 200000000; i++) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//string.Equals(a, b);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a.Equals(b);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;stopper.Stop();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Time: {0}&amp;quot;, stopper.ElapsedMilliseconds);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;} &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;a.Equals(b) - takes around 1650 ms.&lt;/p&gt;
&lt;p&gt;string.Equals(a,b) - takes around 4900 ms.&lt;/p&gt;
&lt;p&gt;So what is the catch?&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8375803</link><pubDate>Thu, 10 Apr 2008 21:15:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8375803</guid><dc:creator>shareef</dc:creator><description>&lt;p&gt;Elz is correct on my Dual Xeon procesor,&lt;/p&gt;
&lt;p&gt;a.Equals(b) - 1409 ms&lt;/p&gt;
&lt;p&gt;string.Equals(a,b) - 1610 ms&lt;/p&gt;
&lt;p&gt;a == b - 1610 ms&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8375813</link><pubDate>Thu, 10 Apr 2008 21:18:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8375813</guid><dc:creator>shareef</dc:creator><description>&lt;p&gt;Also, c# reference says string == operator checks for values not reference.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://msdn2.microsoft.com/en-us/library/53k8ybth.aspx"&gt;http://msdn2.microsoft.com/en-us/library/53k8ybth.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8472993</link><pubDate>Thu, 08 May 2008 20:16:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8472993</guid><dc:creator>itsmarik</dc:creator><description>&lt;p&gt;Here is what I found...&lt;/p&gt;
&lt;p&gt;If a=b, string.Equals(a, b) is fastest. &amp;nbsp;If a &amp;lt;&amp;gt; b or a.Length &amp;lt;&amp;gt; b.Length, a.Equals(b) is fastest. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;The example Elz gave, a and b have different lengths.&lt;/p&gt;
</description></item><item><title>Probably something missed</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8668831</link><pubDate>Mon, 30 Jun 2008 01:55:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8668831</guid><dc:creator>tomas</dc:creator><description>&lt;p&gt;When looking on this results think that it is not better to use static Equals() - is fastest only if strings are same - or is good only if we just testing and expecting that string equals, but always - it plays role only in case of huge usage of string comparisons.&lt;/p&gt;
&lt;p&gt;This are meassures from testing code:&lt;/p&gt;
&lt;p&gt; * m1 = first method a.Equals(b)&lt;/p&gt;
&lt;p&gt; * m2 = second method string.Equals(a, b)&lt;/p&gt;
&lt;p&gt; (using == operator is almost same then m2, but always very little slower)&lt;/p&gt;
&lt;p&gt;CNT = 20.000.000&lt;/p&gt;
&lt;p&gt;1. different length : m1=199 &amp;nbsp;m2=216&lt;/p&gt;
&lt;p&gt;2. same length but different: m1=473 &amp;nbsp;m2=499&lt;/p&gt;
&lt;p&gt;3. same strings : m1=485 &amp;nbsp;m2=118&lt;/p&gt;
&lt;p&gt;CNT = 2.000.000.000&lt;/p&gt;
&lt;p&gt;1. different length : m1=18708 &amp;nbsp;m2=21120&lt;/p&gt;
&lt;p&gt;2. same length but different: m1=47607 m2=49944&lt;/p&gt;
&lt;p&gt;3. same strings : m1=48626 &amp;nbsp;m2=11262&lt;/p&gt;
</description></item><item><title>Stringing along in .NET&amp;#8230; &amp;laquo; akutz&amp;#8217;s blog</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8758894</link><pubDate>Sun, 20 Jul 2008 19:57:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8758894</guid><dc:creator>Stringing along in .NET&amp;#8230; &amp;laquo; akutz&amp;#8217;s blog</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://akutz.wordpress.com/2008/07/20/stringing-along-in-net/"&gt;http://akutz.wordpress.com/2008/07/20/stringing-along-in-net/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8759823</link><pubDate>Mon, 21 Jul 2008 03:22:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8759823</guid><dc:creator>Schley Andrew Kutz</dc:creator><description>&lt;p&gt;I did some research on the topic at &lt;a rel="nofollow" target="_new" href="http://akutz.wordpress.com/2008/07/20/stringing-along-in-net/"&gt;http://akutz.wordpress.com/2008/07/20/stringing-along-in-net/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8759824</link><pubDate>Mon, 21 Jul 2008 03:22:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8759824</guid><dc:creator>Schley Andrew Kutz</dc:creator><description>&lt;p&gt;I did some research on the topic at &lt;a rel="nofollow" target="_new" href="http://akutz.wordpress.com/2008/07/20/stringing-along-in-net/"&gt;http://akutz.wordpress.com/2008/07/20/stringing-along-in-net/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#8788543</link><pubDate>Tue, 29 Jul 2008 15:42:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8788543</guid><dc:creator>MrY</dc:creator><description>&lt;p&gt;I think the string comparison optimizatioin is not so useful.&lt;/p&gt;
&lt;p&gt;I wound rather to use the &amp;quot;a.Equals(b)&amp;quot;.&lt;/p&gt;
&lt;p&gt;I think the optimization of string contains() or indexof() is more useful.&lt;/p&gt;
</description></item><item><title>re: C#: Fast string comparison optimization</title><link>http://blogs.msdn.com/abhinaba/archive/2006/01/07/510169.aspx#9010101</link><pubDate>Tue, 21 Oct 2008 20:51:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9010101</guid><dc:creator>Jan</dc:creator><description>&lt;p&gt;The reason is, that both String.Equals(Object) and String.Equals(String) are virtual, thus cannot be inlined, whilst static String.Equals(String,String) can. &lt;/p&gt;
</description></item></channel></rss>