Performant reverse string extension method

by John Nye

05 Nov
2014

Reversing a string efficiently

As part of a new feature on a nuget package I am working on I wanted to reverse a string into a new string. A simple request I thought, and indeed it was. Using System.Linq I was able to use the .Reverse() extension method.

var reversed = value.Reverse()

Wait a minute... my reversed variable isn't a string, it's IEnumerable<char>. No problem, simply call .ToString(). In most cases this would be fine but for my particular scenario performance was important so I went about creating a simple alternative:

public static class StringExtensions
{
    public static string ReverseString(this string value)
    {
        var sb = new StringBuilder(value.Length);
        for (int i = value.Length -1; i >= 0; i--)
        {
            sb.Append(value[i]);
        }
        return sb.ToString();
    }
}

After running some simple tests that reversed a 50 character string, the results were pleasing. The timings themselves differed however, the ReverseString() method consistently out performed the Reverse().ToString() method

An example of some of the test output shows that the time saved is not insignificant especially when you start dealing with millions of strings:

ReverseString test results:
.Reverse().ToString() time taken: 00:00:00.0017556
.ReverseString() time taken:      00:00:00.0003042

UPDATE

My old buddy @paulthecyclist has managed to put forward an even quicker implementation of this functionality. Below is a quick summary on his implementation and how that compares to the above.

The Method

public static string ArrayReverse(this string value)
{
    var charArray = value.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}

The method takes advantage of Array.Reverse() which as Paul points out is different to the Linq extension method on IEnumerable. This method is also much cleaner and simpler to read.

Performance

ReverseArray test results:
.ReverseString() time taken:      00:00:00.0003042
.ArrayReverse() time taken:       00:00:00.0002265

As you can see, the improvements in speed are quite substantial and will add up when performing this on multiple strings

Thanks to @paulthecyclist for this addition. Be sure to go and check out his blog: http://paulthecyclist.com/


If you would like to get in contact, please do so by adding a comment below or you can contact me on twitter (@ninjanye)

Comments 2

paulthecyclist says: 3445 days ago

Interesting post - made me think.

I think your extension can be made quicker, if it made use of Array.Reverse (note this is different from IEnumerable.Reverse<>(). For me this method seems about 33% quicker than your posted method.

public static string PaulTheCyclistReverse(this string value)
    {
        var result = value.ToCharArray();
        Array.Reverse(result);
        return new string(result);           
    }

Was quicker on my machine, anyhow :) Happy to write a post explaining why if you like.

Cheers

Paul

John says: 3444 days ago

Nice, cheers @Paul,

I'll update the post with the improved method

Leave a message...

18 Apr
2024