using System; using System.Text; using System.Collections.Generic; using System.Diagnostics; public class Test { public static void Main() { string Text = GetText(50000); List Indexes = GetIndexes(2000, Text.Length); Stopwatch clean1 = new Stopwatch(); clean1.Start(); string clean1Result = null; for (int i = 0; i < 100; i++) { clean1Result = Clean1(Text, Indexes); } clean1.Stop(); Stopwatch clean2 = new Stopwatch(); clean2.Start(); string clean2Result = null; for (int i = 0; i < 100; i++) { clean2Result = Clean2(Text, Indexes); } clean2.Stop(); Stopwatch clean3 = new Stopwatch(); clean3.Start(); string clean3Result = null; for (int i = 0; i < 100; i++) { clean3Result = Clean3(Text, Indexes); } clean3.Stop(); Console.WriteLine("Clean 1: {0}", clean1.ElapsedMilliseconds); Console.WriteLine("Clean 2: {0}", clean2.ElapsedMilliseconds); Console.WriteLine("Clean 3: {0}", clean3.ElapsedMilliseconds); } public static string GetText(int length) { var random = new Random(); StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { sb.Append(random.Next(0, 10)); } return sb.ToString(); } public static List GetIndexes(int length, int textLength) { var random = new Random(); var list = new List(length); int max = (int)(textLength * 1.5); for (int i = 0; i < length; i++) { list.Add(random.Next(0, max)); } return list; } public static string Clean1(string Text, List Indexes) { HashSet hashSet = new HashSet(Indexes); StringBuilder sb = new StringBuilder(Text.Length); for (int i = 0; i < Text.Length; ++i) { if (!hashSet.Contains(i)) { sb.Append(Text[i]); } } return sb.ToString(); } public static string Clean2(string Text, List Indexes) { const char temp = '\uFFF0'; StringBuilder sb = new StringBuilder(Text); for (int i = 0; i < Indexes.Count; i++) { if (Indexes[i] < sb.Length) { sb[Indexes[i]] = temp; } } return sb.Replace(temp.ToString(), null).ToString(); } public static string Clean3(string Text, List Indexes) { bool[] exclude = new bool[Text.Length]; for (int i = 0; i < Indexes.Count; i++) { if (Indexes[i] < exclude.Length) { exclude[Indexes[i]] = true; } } StringBuilder sb = new StringBuilder(Text.Length); for (int i = 0; i < Text.Length; i++) { if (!exclude[i]) { sb.Append(Text[i]); } } return sb.ToString(); } }