code_junkie

Is String.IndexOf(char) really slower than manual searching?

Nov 13th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. const string str = @"91023m lkajsdfl;jkasdf;piou-09324\adf asdf45 65u 86 8\;";
  2. static int testIndexOf() { return str.IndexOf('\'); }
  3. static int testManualIndexOf() {
  4. string s = str;
  5. for (int i = 0; i < s.Length; ++i)
  6. if (s[i] == '\') return i;
  7. return -1;
  8. }
  9.  
  10. - String. IndexOf : 00:00:07.6490042
  11. - MyString.PublicIndexOf : 00:00:05.6676471
  12. - MyString.InternIndexOf : 00:00:06.1191796
  13. - MyString.PublicIndexOf2: 00:00:09.1363687
  14. - MyString.InternIndexOf2: 00:00:09.1182569
  15.  
  16. public static class MyString {
  17. public static int PublicIndexOf(string str, char value) {
  18. for (int i = 0; i < str.Length; ++i)
  19. if (str[i] == value) return i;
  20. return -1;
  21. }
  22.  
  23. internal static int InternIndexOf(string str, char value) {
  24. for (int i = 0; i < str.Length; ++i)
  25. if (str[i] == value) return i;
  26. return -1;
  27. }
  28.  
  29. public static int PublicIndexOf2(string str, char value, int startIndex) {
  30. if (startIndex < 0 || startIndex >= str.Length)
  31. throw new ArgumentOutOfRangeException("startIndex");
  32. for (; startIndex < str.Length; ++startIndex)
  33. if (str[startIndex] == value) return startIndex;
  34. return -1;
  35. }
  36.  
  37. internal static int InternIndexOf2(string str, char value, int startIndex) {
  38. if (startIndex < 0 || startIndex >= str.Length)
  39. throw new ArgumentOutOfRangeException("startIndex");
  40. for (; startIndex < str.Length; ++startIndex)
  41. if (str[startIndex] == value) return startIndex;
  42. return -1;
  43. }
  44. }
  45.  
  46. class Program {
  47. static void Main(string[] args) {
  48. int iterations = 100 * 1000 * 1000; // 100 millions
  49. char separator = '\';
  50. string str = @"91023m lkajsdfl;jkasdf;piou-09324\adf asdf45 65u 86 8\;";
  51. Stopwatch watch = new Stopwatch();
  52.  
  53. // test String.IndexOf
  54. int sum = 0;
  55. watch.Start();
  56. for (int i = 0; i < iterations; ++i)
  57. sum += str.IndexOf(separator);
  58. watch.Stop();
  59. Console.WriteLine(" String. IndexOf : ({0}, {1})", watch.Elapsed, sum);
  60.  
  61. // test MyString.PublicIndexOf
  62. sum = 0;
  63. watch.Reset(); watch.Start();
  64. for (int i = 0; i < iterations; ++i)
  65. sum += MyString.PublicIndexOf(str, separator);
  66. watch.Stop();
  67. Console.WriteLine("MyString.PublicIndexOf : ({0}, {1})", watch.Elapsed, sum);
  68.  
  69. // test MyString.InternIndexOf
  70. sum = 0;
  71. watch.Reset(); watch.Start();
  72. for (int i = 0; i < iterations; ++i)
  73. sum += MyString.InternIndexOf(str, separator);
  74. watch.Stop();
  75. Console.WriteLine("MyString.InternIndexOf : ({0}, {1})", watch.Elapsed, sum);
  76.  
  77. // test MyString.PublicIndexOf2
  78. sum = 0;
  79. watch.Reset(); watch.Start();
  80. for (int i = 0; i < iterations; ++i)
  81. sum += MyString.PublicIndexOf2(str, separator,0);
  82. watch.Stop();
  83. Console.WriteLine("MyString.PublicIndexOf2: ({0}, {1})", watch.Elapsed, sum);
  84.  
  85. // test MyString.InternIndexOf2
  86. sum = 0;
  87. watch.Reset(); watch.Start();
  88. for (int i = 0; i < iterations; ++i)
  89. sum += MyString.InternIndexOf2(str, separator,0);
  90. watch.Stop();
  91. Console.WriteLine("MyString.InternIndexOf2: ({0}, {1})", watch.Elapsed, sum);
  92. }
  93. }
  94.  
  95. String. IndexOf : (00:00:06.3134669, -994967296)
  96. MyString.PublicIndexOf : (00:00:07.0769368, -994967296)
  97. MyString.InternIndexOf : (00:00:08.3463652, -994967296)
  98. MyString.PublicIndexOf2: (00:00:12.0049268, -994967296)
  99. MyString.InternIndexOf2: (00:00:12.4344756, -994967296)
  100.  
  101. static void Main()
  102. {
  103. const int LOOP = 25000000;
  104. int chk1 = 0, chk2 = 0;
  105. Stopwatch watch1 = Stopwatch.StartNew();
  106. for (int i = 0; i < LOOP; i++)
  107. {
  108. chk1 += testIndexOf();
  109. }
  110. watch1.Stop();
  111. Stopwatch watch2 = Stopwatch.StartNew();
  112. for (int i = 0; i < LOOP; i++)
  113. {
  114. chk2 += testManualIndexOf();
  115. }
  116. watch2.Stop();
  117.  
  118. Console.WriteLine("{0}ms, ({1})", watch1.ElapsedMilliseconds, chk1);
  119. Console.WriteLine("{0}ms, ({1})", watch2.ElapsedMilliseconds, chk2);
  120.  
  121. }
  122.  
  123. static void Main(string[] args)
  124. {
  125. {
  126. Stopwatch sw = new Stopwatch();
  127. sw.Start();
  128. for (int i = 0; i < 25000000; i++)
  129. testIndexOf();
  130. sw.Stop();
  131. Console.WriteLine(sw.Elapsed); //2.27 s
  132. }
  133. {
  134. Stopwatch sw = new Stopwatch();
  135. sw.Start();
  136. for (int i = 0; i < 25000000; i++)
  137. testManualIndexOf();
  138. sw.Stop();
  139. Console.WriteLine(sw.Elapsed); //9.81 s
  140. }
  141.  
  142. Console.ReadLine();
  143. }
  144.  
  145. public static void Main(string[] args) {
  146. int nTrials = 10000000;
  147. Random rg = new Random(1);
  148. Stopwatch sw = new Stopwatch();
  149. for (int i = 0; i < nTrials; i++) {
  150. int len = 1 + rg.Next(250);
  151. char[] randomChar = new char[len];
  152. for(int j = 0; j < len; j++) {
  153. randomChar[j] = GetRandomLetterOrDigit(rg);
  154. }
  155. string s = new string(randomChar);
  156. char findChar = GetRandomLetterOrDigit(rg);
  157. sw.Start();
  158. int index = s.IndexOf(findChar);
  159. //int index = testManualIndexOf(s, findChar);
  160. sw.Stop();
  161. }
  162. Console.WriteLine(sw.ElapsedMilliseconds);
  163. }
  164.  
  165. private static char GetRandomLetterOrDigit(Random rg) {
  166. char c;
  167. int rc = rg.Next(62);
  168.  
  169. if (rc < 26) {
  170. c = (char)('A' + rc);
  171. }
  172. else if (rc < 52) {
  173. c = (char)('a' + rc - 26);
  174. }
  175. else {
  176. c = (char)('0' + rc - 52);
  177. }
  178. return c;
  179. }
  180.  
  181. private static int testManualIndexOf(string s, char c) {
  182. if (s == null) {
  183. throw new ArgumentNullException("s");
  184. }
  185. for (int i = 0; i < s.Length; ++i) {
  186. if (s[i] == c) return i;
  187. }
  188. return -1;
  189. }
Add Comment
Please, Sign In to add comment