Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Reverse a string - Performance calculation in C#

The following code explains the performance of each method that we used in forget code.

Result from the code below :

For a short string, we can go for EXOR operation.
For a long string, we can go for Array reverse.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace forgetCode
{
    class Program
    {
        delegate string StringDelegate(string s);

        static void Benchmark(string description, StringDelegate d, int times, string text)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int j = 0; j < times; j++)
            {
                d(text);
            }
            sw.Stop();
            Console.WriteLine("{0} Ticks {1} : called {2} times.", sw.ElapsedTicks, description, times);
        }

        public static string ReverseXor(string s)
        {
            char[] charArray = s.ToCharArray();
            int len = s.Length - 1;

            for (int i = 0; i < len; i++, len--)
            {
                charArray[i] ^= charArray[len];
                charArray[len] ^= charArray[i];
                charArray[i] ^= charArray[len];
            }

            return new string(charArray);
        }

        public static string ReverseSB(string text)
        {
            StringBuilder builder = new StringBuilder(text.Length);
            for (int i = text.Length - 1; i >= 0; i--)
            {
                builder.Append(text[i]);
            }
            return builder.ToString();
        }

        public static string ReverseArray(string text)
        {
            char[] array = text.ToCharArray();
            Array.Reverse(array);
            return (new string(array));
        }

        public static string StringOfLength(int length)
        {
            Random random = new Random();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))));
            }
            return sb.ToString();
        }

        static void Main(string[] args)
        {

            int[] lengths = new int[] { 1, 10, 15, 25, 50, 75, 100, 1000, 100000 };

            foreach (int l in lengths)
            {
                int iterations = 10000;
                string text = StringOfLength(l);
                Benchmark(String.Format("String Builder (Length: {0})", l), ReverseSB, iterations, text);
                Benchmark(String.Format("Array.Reverse (Length: {0})", l), ReverseArray, iterations, text);
                Benchmark(String.Format("Xor (Length: {0})", l), ReverseXor, iterations, text);

                Console.WriteLine();
            }

            Console.Read();
        }
    }
}


Output:
2840 Ticks String Builder (Length: 1) : called 10000 times.
2900 Ticks Array.Reverse (Length: 1) : called 10000 times.
1785 Ticks Xor (Length: 1) : called 10000 times.

4439 Ticks String Builder (Length: 10) : called 10000 times.
3388 Ticks Array.Reverse (Length: 10) : called 10000 times.
2789 Ticks Xor (Length: 10) : called 10000 times.

4748 Ticks String Builder (Length: 15) : called 10000 times.
2699 Ticks Array.Reverse (Length: 15) : called 10000 times.
2887 Ticks Xor (Length: 15) : called 10000 times.

7513 Ticks String Builder (Length: 25) : called 10000 times.
3009 Ticks Array.Reverse (Length: 25) : called 10000 times.
4210 Ticks Xor (Length: 25) : called 10000 times.

12868 Ticks String Builder (Length: 50) : called 10000 times.
3771 Ticks Array.Reverse (Length: 50) : called 10000 times.
7632 Ticks Xor (Length: 50) : called 10000 times.

19079 Ticks String Builder (Length: 75) : called 10000 times.
4791 Ticks Array.Reverse (Length: 75) : called 10000 times.
10619 Ticks Xor (Length: 75) : called 10000 times.

24331 Ticks String Builder (Length: 100) : called 10000 times.
5496 Ticks Array.Reverse (Length: 100) : called 10000 times.
13903 Ticks Xor (Length: 100) : called 10000 times.

223442 Ticks String Builder (Length: 1000) : called 10000 times.
35773 Ticks Array.Reverse (Length: 1000) : called 10000 times.
125728 Ticks Xor (Length: 1000) : called 10000 times.

25980361 Ticks String Builder (Length: 100000) : called 10000 times.
4596920 Ticks Array.Reverse (Length: 100000) : called 10000 times.
13449519 Ticks Xor (Length: 100000) : called 10000 times.