fork(1) 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(30000);
  11. List<int> Indexes = GetIndexes(1000, Text.Length);
  12.  
  13. Stopwatch clean1 = new Stopwatch();
  14. clean1.Start();
  15. string clean1Result = null;
  16. for (int i = 0; i < 1000; 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 < 1000; i++)
  26. {
  27. clean2Result = Clean2(Text, Indexes);
  28. }
  29. clean2.Stop();
  30.  
  31. Console.WriteLine(clean1Result == clean2Result);
  32. Console.WriteLine("Clean 1: {0}", clean1.ElapsedMilliseconds);
  33. Console.WriteLine("Clean 2: {0}", clean2.ElapsedMilliseconds);
  34. }
  35.  
  36. public static string GetText(int length)
  37. {
  38. var random = new Random();
  39. StringBuilder sb = new StringBuilder(length);
  40. for (int i = 0; i < length; i++)
  41. {
  42. sb.Append(random.Next(0, 10));
  43. }
  44. return sb.ToString();
  45. }
  46.  
  47. public static List<int> GetIndexes(int length, int textLength)
  48. {
  49. var random = new Random();
  50. var list = new List<int>(length);
  51. int max = (int)(textLength * 1.5);
  52. for (int i = 0; i < length; i++)
  53. {
  54. list.Add(random.Next(0, max));
  55. }
  56. return list;
  57. }
  58.  
  59. public static string Clean1(string Text, List<int> Indexes)
  60. {
  61. HashSet<int> hashSet = new HashSet<int>(Indexes);
  62. StringBuilder sb = new StringBuilder(Text.Length);
  63. for (int i = 0; i < Text.Length; ++i)
  64. {
  65. if (!hashSet.Contains(i))
  66. {
  67. sb.Append(Text[i]);
  68. }
  69. }
  70. return sb.ToString();
  71. }
  72.  
  73. public static string Clean2(string Text, List<int> Indexes)
  74. {
  75. const char temp = '\uFFF0';
  76. StringBuilder sb = new StringBuilder(Text);
  77. for (int i = 0; i < Indexes.Count; i++)
  78. {
  79. if (Indexes[i] < sb.Length)
  80. {
  81. sb[Indexes[i]] = temp;
  82. }
  83. }
  84. return sb.Replace(temp.ToString(), null).ToString();
  85. }
  86. }
Success #stdin #stdout 2.56s 23968KB
stdin
Standard input is empty
stdout
True
Clean 1: 2060
Clean 2: 446