THE SQL Server Blog Spot on the Web

Welcome to - The SQL Server blog spot on the web Sign in | |
in Search

Adam Machanic

Adam Machanic, Boston-based SQL Server developer, shares his experiences with programming, monitoring, and performance tuning SQL Server. And the occasional battle with the query optimizer.

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)

            return sb.ToString();

        public static string Reverse2(string s)
            char[] rev = s.ToCharArray();
            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)

            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: ,



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
New Comments to this post are disabled

About Adam Machanic

Adam Machanic is a Boston-based SQL Server developer, writer, and speaker. He focuses on large-scale data warehouse performance and development, and is author of the award-winning SQL Server monitoring stored procedure, sp_WhoIsActive. 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 "SQL Server 2008 Internals" (Microsoft Press, 2009) and "Expert SQL Server 2005 Development" (Apress, 2007). Adam regularly speaks at conferences and training events on a variety of SQL Server topics. He is a Microsoft Most Valuable Professional (MVP) for SQL Server, a Microsoft Certified IT Professional (MCITP), and an alumnus of the INETA North American Speakers Bureau.

This Blog


Privacy Statement