<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.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>More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx</link><description>In the last installment, I showed a potentially fastest method using Array.Reverse. After finding and fixing a bug in method #3 posted in my last installment (it is, in fact, quite a bit faster than method #1 when you don't have a big huge bug in the</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx#122</link><pubDate>Tue, 18 Jul 2006 06:24:22 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:122</guid><dc:creator>Mladen</dc:creator><description>have you tried this one?&lt;br&gt;&lt;br&gt;public string Reverse(string x)&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;char[] charArray = new char[x.Length];&lt;br&gt; &amp;nbsp; &amp;nbsp;int len = x.Length - 1;&lt;br&gt; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt;= len; i++)&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;charArray[i] = x[len-i];&lt;br&gt; &amp;nbsp; &amp;nbsp;return new string(charArray);&lt;br&gt;}&lt;br&gt;</description></item><item><title>re: More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx#123</link><pubDate>Tue, 18 Jul 2006 23:20:41 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:123</guid><dc:creator>Adam Machanic</dc:creator><description>Mladen: &lt;br&gt;&lt;br&gt;Just tested it. &amp;nbsp;It falls halfway between methods 2 and 3 (2 being the 2nd fastest, after 5).&lt;br&gt;&lt;br&gt;</description></item><item><title>re: More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx#124</link><pubDate>Wed, 19 Jul 2006 05:52:03 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:124</guid><dc:creator>Mladen</dc:creator><description>hmm... that's interesting...&lt;br&gt;&lt;br&gt;everytime i try it that one was faster than XOR.&lt;br&gt;</description></item><item><title>re: More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx#125</link><pubDate>Wed, 19 Jul 2006 14:31:22 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:125</guid><dc:creator>Adam Machanic</dc:creator><description>How are you testing, and what are you using to get the timings?&lt;br&gt;&lt;br&gt;I am simply running every method in a loop 10000 times:&lt;br&gt;&lt;br&gt;for (int i = 0; i&amp;lt;10000; i++)&lt;br&gt;{&lt;br&gt; &amp;nbsp;string s1 = Reverse1(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt; &amp;nbsp;string s2 = Reverse2(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt; &amp;nbsp;string s3 = Reverse3(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt; &amp;nbsp;string s4 = Reverse4(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt; &amp;nbsp;string s5 = Reverse5(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt; &amp;nbsp;string s6 = Reverse6(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt;}&lt;br&gt;&lt;br&gt;I'm getting the timings using VS's profiler, in instrumentation mode, and looking at the &amp;quot;inclusive of children&amp;quot; column to get the final reported times.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx#126</link><pubDate>Thu, 20 Jul 2006 11:34:18 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:126</guid><dc:creator>Mladen</dc:creator><description>well i simply do it like this:&lt;br&gt;string testString = &amp;quot;1234567890hguidagnrruiasngilrenglirengilrenglaes&amp;quot;;&lt;br&gt;DateTime dtStart = DateTime.Now;&lt;br&gt;for (int i = 0; i &amp;lt; 1000000; i++)&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;Reverse1(testString);&lt;br&gt;}&lt;br&gt;DateTime dtEnd = DateTime.Now;&lt;br&gt;TimeSpan ts = dtEnd - dtStart;&lt;br&gt;&lt;br&gt;dtStart = DateTime.Now;&lt;br&gt;for (int i = 0; i &amp;lt; 1000000; i++)&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;Reverse2(testString);&lt;br&gt;}&lt;br&gt;dtEnd = DateTime.Now;&lt;br&gt;ts = dtEnd - dtStart;&lt;br&gt;&lt;br&gt;public string Reverse1(string x)&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;char[] charArray = new char[x.Length];&lt;br&gt; &amp;nbsp; &amp;nbsp;int len = x.Length - 1;&lt;br&gt; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt;= len; i++)&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;charArray[i] = x[len - i];&lt;br&gt; &amp;nbsp; &amp;nbsp;return new string(charArray);&lt;br&gt;}&lt;br&gt;&lt;br&gt;public string Reverse2(string str)&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;// convert the string to char array&lt;br&gt; &amp;nbsp; &amp;nbsp;char[] charArray = str.ToCharArray();&lt;br&gt; &amp;nbsp; &amp;nbsp;int len = str.Length - 1;&lt;br&gt; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; len; i++, len--)&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;charArray[i] ^= charArray[len];&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;charArray[len] ^= charArray[i];&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;charArray[i] ^= charArray[len];&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;return new string(charArray);&lt;br&gt;}&lt;br&gt;&lt;br&gt;is there something wrong with this way that i'm not familiar with?&lt;br&gt;&lt;br&gt;An average time i get is around 650 ms for reverse1 and 850 for reverse2.&lt;br&gt;It doesn't matter how many times i run it.</description></item><item><title>re: More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx#127</link><pubDate>Thu, 20 Jul 2006 15:55:08 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:127</guid><dc:creator>Adam Machanic</dc:creator><description>The problem with that method is that it's &amp;quot;wall clock&amp;quot; time -- during the middle of invocation of one of the methods the GC could kick in, some other unrelated process might require a bunch of time, etc -- that will skew the results. &amp;nbsp;The profiler, AFAIK, shows only time spent actually doing your task, so you don't have all of the other noise and get more accurate results.&lt;br&gt;&lt;br&gt;That said, I went ahead and tried this method and here are my results (note, I ran each method 2 million times -- I was unable to get consistent results otherwise):&lt;br&gt;&lt;br&gt;Reverse1: 1892.7216 ms&lt;br&gt;Reverse2: 660.9504 ms&lt;br&gt;Reverse3: 1892.7216 ms&lt;br&gt;Reverse4: 4196.0336 ms&lt;br&gt;Reverse5: 490.7056 ms&lt;br&gt;Reverse6: 2042.9376 ms&lt;br&gt;Reverse7: 510.7344 ms&lt;br&gt;&lt;br&gt;Note that Reverse5 is your XOR method and Reverse7 is your new method. &amp;nbsp;So at least on my box, XOR still wins.&lt;br&gt;&lt;br&gt;Here is the code I used to loop:&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DateTime theStart = DateTime.Now;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; 1000000; i++)&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string s1 = Reverse1(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;s1 = Reverse1(s1);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DateTime theEnd = DateTime.Now;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TimeSpan ts = theEnd - theStart;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(ts.TotalMilliseconds.ToString());&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;theStart = DateTime.Now;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; 1000000; i++)&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string s2 = Reverse2(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;s2 = Reverse2(s2);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;theEnd = DateTime.Now;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ts = theEnd - theStart;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(ts.TotalMilliseconds.ToString());&lt;br&gt;&lt;br&gt;... (repeat for each up to Reverse7)&lt;br&gt;&lt;br&gt;</description></item><item><title>re: More on string reversal!</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/17/more-on-string-reversal.aspx#128</link><pubDate>Fri, 21 Jul 2006 06:07:53 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:128</guid><dc:creator>Mladen</dc:creator><description>Cool. thanx for explaining that.&lt;br&gt;&lt;br&gt;In the end though... who needs to reverse a string 2 million times, right? :)))&lt;br&gt;&lt;br&gt;If you haven't yet go take a look at how Greg Young did it. &lt;br&gt;It's in the comments:&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://weblogs.sqlteam.com/mladenp/archive/2006/03/19/9350.aspx"&gt;http://weblogs.sqlteam.com/mladenp/archive/2006/03/19/9350.aspx&lt;/a&gt;</description></item></channel></rss>