<?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# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx</link><description>&amp;lt;Also read the later posting on the same topic&amp;gt; 
 Frequently in code we do string comparisons which is culture-agnostic. While we should follow the string comparison guidelines , there is a much faster way of getting it done using string interning</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#1237619</link><pubDate>Fri, 08 Dec 2006 11:43:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1237619</guid><dc:creator>Jimmy James</dc:creator><description>&lt;p&gt;There is a cost when you call IsInterned as well. &amp;nbsp;The implementation of the interning table is a HashTable so every insert and lookup requires the calculation of a hash value. &amp;nbsp;On the other hand a string comparator will return false as soon as two bytes disagree. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Use normal string comparison unless you are in the position of being to hash things once before doing a very large number of comparisons on them.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1237619" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#489411</link><pubDate>Sat, 05 Nov 2005 16:45:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:489411</guid><dc:creator>Manip</dc:creator><description>I don't like this coding style... The reason being that if a maintainer changed a line of code somewhere else in the project (const/compile.string to std.string) then it could break at this point, and every other point when these &amp;quot;performance improvements&amp;quot; have been made.  &lt;br&gt;&lt;br&gt;Worse still it would be VERY difficult to track down unless you knew what to look for... :-/&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=489411" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#488554</link><pubDate>Thu, 03 Nov 2005 07:43:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488554</guid><dc:creator>Abhinaba Basu [MSFT]</dc:creator><description>IsInterned would be too expensive to call from within String.Equals, so that not going to work.&lt;br&gt;&lt;br&gt;On loading your assembly all the literal strings are interned and which could be 100's of string. This is load time perf-hit which is OK. However, the explicit Intern call is run-time. However, if its done once say in Main for the command string and used for comparison 100's of times then the gain is significant....&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488554" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#488119</link><pubDate>Wed, 02 Nov 2005 14:30:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488119</guid><dc:creator>Ron Krauter</dc:creator><description>I was getting excited till I reached this line : &lt;br&gt;However, the catch is that the command has to be an interned string.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488119" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#488081</link><pubDate>Wed, 02 Nov 2005 11:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488081</guid><dc:creator>Stuart Ballard</dc:creator><description>I knew that *intern* has a perf hit; I was wondering whether IsInterned has the same perf hit or whether that could be done cheaply. If IsInterned is cheap, it would be possible to use it inside the standard equality operator for System.String so that interned strings compare fast with &amp;quot;==&amp;quot; while non-interned strings fall back to the slower path.&lt;br&gt;&lt;br&gt;If IsInterned is expensive of course that trick doesn't work...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488081" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#488078</link><pubDate>Wed, 02 Nov 2005 11:10:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488078</guid><dc:creator>Abhinaba Basu [MSFT]</dc:creator><description>This works mainly for interned string. &lt;br&gt;Call to string.intern does have a performance hit. This'll work only if you need to intern the string say once and use comparisons multiple times with long strings. As with most optimization you need to profile and see if this is making a good difference to your perf and then use it.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488078" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#488060</link><pubDate>Wed, 02 Nov 2005 09:57:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488060</guid><dc:creator>Morten</dc:creator><description>Isn't the whole performance idea lost the moment you call string.intern and ToUpper() ?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488060" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#488058</link><pubDate>Wed, 02 Nov 2005 09:44:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488058</guid><dc:creator>Stuart Ballard</dc:creator><description>I wonder how fast the IsInterned check is. If it's fast it would have been really nice for Microsoft to add code like this into the BCL:&lt;br&gt;&lt;br&gt;public static operator ==(string a, string b) {&lt;br&gt;  string aint = a.IsInterned;&lt;br&gt;  string bint = b.IsInterned;&lt;br&gt;  if (aint != null &amp;amp;&amp;amp; bint != null) return object.ReferenceEquals(aint, bint);&lt;br&gt;  // ... rest of the existing implementation&lt;br&gt;}&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488058" width="1" height="1"&gt;</description></item><item><title>re: C# 2.0: Fast string comparison with string-interning</title><link>http://blogs.msdn.com/b/abhinaba/archive/2005/11/02/488041.aspx#488042</link><pubDate>Wed, 02 Nov 2005 08:22:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:488042</guid><dc:creator>Abhinaba Basu [MSFT]</dc:creator><description>I forgot to add that as always code for clarity and easy maintainability. Profile to see perf-bottlenecks and only then use optmizations as these....&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=488042" width="1" height="1"&gt;</description></item></channel></rss>