THE SQL Server Blog Spot on the Web
Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

Adam Machanic

Adam Machanic, Boston-based independent database consultant, writer, and speaker, shares his experiences with programming, performance tuning, and optimizing SQL Server 2000, 2005, and 2008, in conjunction with related technologies such as .NET.

Reversing a string in .NET

Over in the Simple-Talk forums, there is a good thread going about how best to reverse a string in .NET, since no string reverse method is included in the BCL.

A few suggestions were made, and someone implied that they were too complex and that simplicity is the most important factor.  Personally, I wonder -- does complexity really matter in this case?  I would expect that you'd program this kind of thing exactly once, put it into a utility class somewhere, and never worry about the implementation again.  Given that assumption, it becomes more of a performance question for me.

To that end, I've run a few tests.  I created three methods -- the first is a method using StringBuilder, as suggested by one of the respondants in the thread who felt it was the simplest method of achieving tho goal.  The second uses Array.Reverse, and is perhaps slightly more obscure.  The third method is a modification of the first, in which the main loop iterates over an array of chars instead of the string, which I had a hunch would be faster (alas, tests show that I was not correct in my assumption).  Following are the methods:

        public static string Reverse1(string s)
        {
            StringBuilder sb = new System.Text.StringBuilder(s.Length);
            int i = s.Length;

            while (i-- > 0)
            {
                sb.Append(s[i]);
            }

            return sb.ToString();
        }

        public static string Reverse2(string s)
        {
            char[] rev = s.ToCharArray();
            Array.Reverse(rev);
            return (new string(rev));
        }

        public static string Reverse3(string s)
        {
            char[] charArray = s.ToCharArray();

            int i = charArray.Length;

            StringBuilder sb = new System.Text.StringBuilder(i);           

            while (i-- > 0)
            {
                sb.Append(s[i]);
            }

            return sb.ToString();
        }


I turned on Visual Studio's profiler to collect timings and ran these methods in a loop.  First test was reversing 26 characters (a-z), which I ran in a loop 10,000 times for each method.  Results:

Reverse1: 506.775 ms
Reverse2: 58.327 ms
Reverse3: 522.484 ms

The second test was reversing 2080 characters (a-z repeated 80 times).  I was only able to run this test in the loop 2500 times, as my workstation is currently very low on disk space and the VS profiler's output takes up a lot of room.  Results:

Reverse1: 2060.424 ms
Reverse2: 117.874 ms
Reverse3: 2368.861 ms

Results are total time spent in each function and its children over the course of all iterations.

So for me, the Array.Reverse method is what I would use... Anyone out there have a better version to share?



Published Monday, July 17, 2006 12:49 PM by Adam Machanic
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Adam Machanic said:

In the last installment, I showed a potentially fastest method using Array.Reverse.After finding and...
July 17, 2006 7:27 PM
 

Adam Machanic said:

In the last installment, I showed a potentially fastest method using Array.Reverse. After finding and

January 8, 2007 1:24 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Adam Machanic

Adam Machanic is a Boston-based independent database consultant, writer, and speaker. He has been involved in dozens of SQL Server implementations for both high-availability OLTP and large-scale data warehouse applications, and has optimized data access layer performance for several data-intensive applications. Adam has written for numerous web sites and magazines, including SQLblog, Simple Talk, Search SQL Server, SQL Server Professional, CoDe, and VSJ. He has also contributed to several books on SQL Server, including "Expert SQL Server 2005 Development" (Apress, 2007) and "Inside SQL Server 2005: Query Tuning and Optimization" (Microsoft Press, 2007). Adam regularly speaks at user groups, community events, and conferences on a variety of SQL Server and .NET-related topics. He is a Microsoft Most Valuable Professional (MVP) for SQL Server and a Microsoft Certified IT Professional (MCITP).
Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement