最近项目中经常会用到去重功能,写了一段测试代码,测试下同等数据量下HashSet、Dictionary、Hashtable三种类型的Contains效率
Stopwatch watch = null;
int max = 5000000;
int searchStart = 1555555;
int searchEnd = 4555555;
//Start******************
watch = Stopwatch.StartNew();
HashSet<string> hashSet = new HashSet<string>();
for (int i = 0; i < max; i++)
{
hashSet.Add("S" + i);
}
Console.WriteLine("HashSet写入:".PadLeft(15) + watch.Elapsed);
watch = Stopwatch.StartNew();
for (int i = searchStart; i < searchEnd; i++)
{
bool ss = hashSet.Contains("S" + i);
if (ss == false)
{
Console.WriteLine("*");