今天在做项目的过程中,发现了算法的性能瓶颈,因为算法用了 public Dictionary<string, List<cKnowledgeLi>> cchooseli = new Dictionary<string, List<cKnowledgeLi>>();其中cKnowledgeLi是大对象,所以在对Dictionary的各种方法的操作性能上存在问题,所以突发奇想,想比对下各个存储之间的各个方法操作的性能,进而优化。废话不多说,直接上代码,
const int COUNT = 10000;
HashSet<int> hashSetOfInts = new HashSet<int>();
Stopwatch stopWatch = new Stopwatch();
Console.WriteLine("对10000次进行处理:");
stopWatch.Start();
List<int> listOfInts = new List<int>();
for (int i = 0; i < COUNT; i++)
{
listOfInts.Add(i);
}
Console.WriteLine("集合添加:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
int value =0;
for (int i = 0; i < listOfInts.Count; i++)
{
value = listOfInts[i];
}
Console.WriteLine("集合For循环:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
int value1 = 0;
var enumerator = listOfInts.GetEnumerator();
while (enumerator.MoveNext())
{
value1 = enumerator.Current;
}
Console.WriteLine("集合迭代器:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
listOfInts.Contains(i);
}
stopWatch.Stop();
Console.WriteLine("集合包含:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
Dictionary<string, string> domainMap = new Dictionary<string, string>();
for (int i = 0; i < COUNT; i++)
{
domainMap.Add(i.ToString(), i.ToString());
}
Console.WriteLine("字典添加:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
string value2 = "";
foreach (string key in domainMap.Keys)
{
value2 = domainMap[key];
}
Console.WriteLine("字典For循环:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
string value3 = "";
var enumerator1 = domainMap.GetEnumerator();
while (enumerator1.MoveNext())
{
value3 = enumerator1.Current.Key;
}
Console.WriteLine("字典迭代器:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
domainMap.ContainsKey(i.ToString());
}
stopWatch.Stop();
Console.WriteLine("字典ContainsKey:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
hashSetOfInts.Add(i);
}
stopWatch.Stop();
Console.WriteLine("HashSet添加:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
int value4 =0;
foreach (var key in hashSetOfInts)
{
value4 = key;
}
Console.WriteLine("HashSetFor循环:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
int value5 = 0;
var enumerator2 = hashSetOfInts.GetEnumerator();
while (enumerator2.MoveNext())
{
value5 = enumerator2.Current;
}
Console.WriteLine("HashSet迭代器:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
hashSetOfInts.Contains(i);
}
stopWatch.Stop();
Console.WriteLine("HashSet包含:" + stopWatch.Elapsed);
stopWatch.Reset();
Console.WriteLine("-------------------------------------------");
Console.WriteLine();
比较效果图: