using System;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
public class Test
{
public static void Main()
{
string Text = GetText(30000);
List<int> Indexes = GetIndexes(1000, Text.Length);
Stopwatch clean1 = new Stopwatch();
clean1.Start();
string clean1Result = null;
for (int i = 0; i < 1000; i++)
{
clean1Result = Clean1(Text, Indexes);
}
clean1.Stop();
Stopwatch clean2 = new Stopwatch();
clean2.Start();
string clean2Result = null;
for (int i = 0; i < 1000; i++)
{
clean2Result = Clean2(Text, Indexes);
}
clean2.Stop();
Console.WriteLine(clean1Result == clean2Result);
Console.WriteLine("Clean 1: {0}", clean1.ElapsedMilliseconds);
Console.WriteLine("Clean 2: {0}", clean2.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<int> GetIndexes(int length, int textLength)
{
var random = new Random();
var list = new List<int>(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<int> Indexes)
{
HashSet<int> hashSet = new HashSet<int>(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<int> 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();
}
}