fork download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4. using System.Diagnostics;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. var sw = new Stopwatch();
  11. string s;
  12. Regex r;
  13.  
  14. const int n = 19;
  15.  
  16. Regex.Replace("any", ".", "x");
  17. "any".AsEnumerable();
  18.  
  19. sw.Restart();
  20. s = "string";
  21. for (int q=0; q<n; ++q) s = Regex.Replace(s, "(.)(?=.)", "$1 ");
  22. sw.Stop();
  23. Console.WriteLine("{0} {1}", s.GetHashCode(), sw.ElapsedMilliseconds);
  24. GC.Collect();
  25.  
  26. sw.Restart();
  27. s = "string";
  28. r = new Regex("(.)(?=.)");
  29. for (int q=0; q<n; ++q) s = r.Replace(s, "$1 ");
  30. sw.Stop();
  31. Console.WriteLine("{0} {1}", s.GetHashCode(), sw.ElapsedMilliseconds);
  32. GC.Collect();
  33.  
  34. sw.Restart();
  35. s = "string";
  36. r = new Regex("(.)(?=.)", RegexOptions.Compiled);
  37. for (int q=0; q<n; ++q) s = r.Replace(s, "$1 ");
  38. sw.Stop();
  39. Console.WriteLine("{0} {1}", s.GetHashCode(), sw.ElapsedMilliseconds);
  40. GC.Collect();
  41.  
  42. sw.Restart();
  43. s = "string";
  44. for (int q=0; q<n; ++q) s = String.Join(" ", s.AsEnumerable());
  45. sw.Stop();
  46. Console.WriteLine("{0} {1}", s.GetHashCode(), sw.ElapsedMilliseconds);
  47. GC.Collect();
  48.  
  49. sw.Restart();
  50. s = "string";
  51. for (int q=0; q<n; ++q) s = String.Join(" ", s.ToCharArray());
  52. sw.Stop();
  53. Console.WriteLine("{0} {1}", s.GetHashCode(), sw.ElapsedMilliseconds);
  54. GC.Collect();
  55. }
  56. }
Success #stdin #stdout 4.22s 134720KB
stdin
Standard input is empty
stdout
455915401 1261
455915401 1261
455915401 1255
455915401 188
455915401 200