fork download
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. string Text = GetText(50000);
  11. List<int> Indexes = GetIndexes(2000, Text.Length);
  12.  
  13. Stopwatch clean1 = new Stopwatch();
  14. clean1.Start();
  15. string clean1Result = null;
  16. for (int i = 0; i < 100; i++)
  17. {
  18. clean1Result = Clean1(Text, Indexes);
  19. }
  20. clean1.Stop();
  21.  
  22. Stopwatch clean2 = new Stopwatch();
  23. clean2.Start();
  24. string clean2Result = null;
  25. for (int i = 0; i < 100; i++)
  26. {
  27. clean2Result = Clean2(Text, Indexes);
  28. }
  29. clean2.Stop();
  30.  
  31. Stopwatch clean3 = new Stopwatch();
  32. clean3.Start();
  33. string clean3Result = null;
  34. for (int i = 0; i < 100; i++)
  35. {
  36. clean3Result = Clean3(Text, Indexes);
  37. }
  38. clean3.Stop();
  39.  
  40. Console.WriteLine("Clean 1: {0}", clean1.ElapsedMilliseconds);
  41. Console.WriteLine("Clean 2: {0}", clean2.ElapsedMilliseconds);
  42. Console.WriteLine("Clean 3: {0}", clean3.ElapsedMilliseconds);
  43. }
  44.  
  45. public static string GetText(int length)
  46. {
  47. var random = new Random();
  48. StringBuilder sb = new StringBuilder(length);
  49. for (int i = 0; i < length; i++)
  50. {
  51. sb.Append(random.Next(0, 10));
  52. }
  53. return sb.ToString();
  54. }
  55.  
  56. public static List<int> GetIndexes(int length, int textLength)
  57. {
  58. var random = new Random();
  59. var list = new List<int>(length);
  60. int max = (int)(textLength * 1.5);
  61. for (int i = 0; i < length; i++)
  62. {
  63. list.Add(random.Next(0, max));
  64. }
  65. return list;
  66. }
  67.  
  68. public static string Clean1(string Text, List<int> Indexes)
  69. {
  70. HashSet<int> hashSet = new HashSet<int>(Indexes);
  71. StringBuilder sb = new StringBuilder(Text.Length);
  72. for (int i = 0; i < Text.Length; ++i)
  73. {
  74. if (!hashSet.Contains(i))
  75. {
  76. sb.Append(Text[i]);
  77. }
  78. }
  79. return sb.ToString();
  80. }
  81.  
  82. public static string Clean2(string Text, List<int> Indexes)
  83. {
  84. const char temp = '\uFFF0';
  85. StringBuilder sb = new StringBuilder(Text);
  86. for (int i = 0; i < Indexes.Count; i++)
  87. {
  88. if (Indexes[i] < sb.Length)
  89. {
  90. sb[Indexes[i]] = temp;
  91. }
  92. }
  93. return sb.Replace(temp.ToString(), null).ToString();
  94. }
  95.  
  96. public static string Clean3(string Text, List<int> Indexes)
  97. {
  98. bool[] exclude = new bool[Text.Length];
  99. for (int i = 0; i < Indexes.Count; i++)
  100. {
  101. if (Indexes[i] < exclude.Length)
  102. {
  103. exclude[Indexes[i]] = true;
  104. }
  105. }
  106. StringBuilder sb = new StringBuilder(Text.Length);
  107. for (int i = 0; i < Text.Length; i++)
  108. {
  109. if (!exclude[i])
  110. {
  111. sb.Append(Text[i]);
  112. }
  113. }
  114. return sb.ToString();
  115. }
  116. }
Success #stdin #stdout 0.6s 24088KB
stdin
Standard input is empty
stdout
Clean 1: 335
Clean 2: 84
Clean 3: 125