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.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace forgetCode
  8. {
  9. class Program
  10. {
  11. delegate string StringDelegate(string s);
  12.  
  13. static void Benchmark(string description, StringDelegate d, int times, string text)
  14. {
  15. Stopwatch sw = new Stopwatch();
  16. sw.Start();
  17. for (int j = 0; j < times; j++)
  18. {
  19. d(text);
  20. }
  21. sw.Stop();
  22. Console.WriteLine("{0} Ticks {1} : called {2} times.", sw.ElapsedTicks, description, times);
  23. }
  24.  
  25. public static string ReverseXor(string s)
  26. {
  27. char[] charArray = s.ToCharArray();
  28. int len = s.Length - 1;
  29.  
  30. for (int i = 0; i < len; i++, len--)
  31. {
  32. charArray[i] ^= charArray[len];
  33. charArray[len] ^= charArray[i];
  34. charArray[i] ^= charArray[len];
  35. }
  36.  
  37. return new string(charArray);
  38. }
  39.  
  40. public static string ReverseSB(string text)
  41. {
  42. StringBuilder builder = new StringBuilder(text.Length);
  43. for (int i = text.Length - 1; i >= 0; i--)
  44. {
  45. builder.Append(text[i]);
  46. }
  47. return builder.ToString();
  48. }
  49.  
  50. public static string ReverseArray(string text)
  51. {
  52. char[] array = text.ToCharArray();
  53. Array.Reverse(array);
  54. return (new string(array));
  55. }
  56.  
  57. public static string StringOfLength(int length)
  58. {
  59. Random random = new Random();
  60. StringBuilder sb = new StringBuilder();
  61. for (int i = 0; i < length; i++)
  62. {
  63. sb.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))));
  64. }
  65. return sb.ToString();
  66. }
  67.  
  68. static void Main(string[] args)
  69. {
  70.  
  71. int[] lengths = new int[] { 1, 10, 15, 25, 50, 75, 100, 1000, 100000 };
  72.  
  73. foreach (int l in lengths)
  74. {
  75. int iterations = 10000;
  76. string text = StringOfLength(l);
  77. Benchmark(String.Format("String Builder (Length: {0})", l), ReverseSB, iterations, text);
  78. Benchmark(String.Format("Array.Reverse (Length: {0})", l), ReverseArray, iterations, text);
  79. Benchmark(String.Format("Xor (Length: {0})", l), ReverseXor, iterations, text);
  80.  
  81. Console.WriteLine();
  82. }
  83.  
  84. Console.Read();
  85. }
  86. }
  87. }


Output:
  1. 2840 Ticks String Builder (Length: 1) : called 10000 times.
  2. 2900 Ticks Array.Reverse (Length: 1) : called 10000 times.
  3. 1785 Ticks Xor (Length: 1) : called 10000 times.
  4.  
  5. 4439 Ticks String Builder (Length: 10) : called 10000 times.
  6. 3388 Ticks Array.Reverse (Length: 10) : called 10000 times.
  7. 2789 Ticks Xor (Length: 10) : called 10000 times.
  8.  
  9. 4748 Ticks String Builder (Length: 15) : called 10000 times.
  10. 2699 Ticks Array.Reverse (Length: 15) : called 10000 times.
  11. 2887 Ticks Xor (Length: 15) : called 10000 times.
  12.  
  13. 7513 Ticks String Builder (Length: 25) : called 10000 times.
  14. 3009 Ticks Array.Reverse (Length: 25) : called 10000 times.
  15. 4210 Ticks Xor (Length: 25) : called 10000 times.
  16.  
  17. 12868 Ticks String Builder (Length: 50) : called 10000 times.
  18. 3771 Ticks Array.Reverse (Length: 50) : called 10000 times.
  19. 7632 Ticks Xor (Length: 50) : called 10000 times.
  20.  
  21. 19079 Ticks String Builder (Length: 75) : called 10000 times.
  22. 4791 Ticks Array.Reverse (Length: 75) : called 10000 times.
  23. 10619 Ticks Xor (Length: 75) : called 10000 times.
  24.  
  25. 24331 Ticks String Builder (Length: 100) : called 10000 times.
  26. 5496 Ticks Array.Reverse (Length: 100) : called 10000 times.
  27. 13903 Ticks Xor (Length: 100) : called 10000 times.
  28.  
  29. 223442 Ticks String Builder (Length: 1000) : called 10000 times.
  30. 35773 Ticks Array.Reverse (Length: 1000) : called 10000 times.
  31. 125728 Ticks Xor (Length: 1000) : called 10000 times.
  32.  
  33. 25980361 Ticks String Builder (Length: 100000) : called 10000 times.
  34. 4596920 Ticks Array.Reverse (Length: 100000) : called 10000 times.
  35. 13449519 Ticks Xor (Length: 100000) : called 10000 times.