什么是最快的收集类型,当我不关心重复,订单等时,我只想添加特定类型的对象,然后通过for循环遍历集合。我不知道会添加多少项目。
我目前使用List(Of)作为数组要求我知道我不知道的大小。但是当集合增长到数百万个对象时,它就成了瓶颈。什么比List中的List(Of)更快/更好?
之前我读过一些类似的问题。但我可能错了,但我觉得像一个hashset是错误的目的,因为一个集合的想法不是我将要做的。像我说的数组要求我在添加项目之前知道大小。字典是错误的目的。
答案 0 :(得分:1)
我不相信List是这样的瓶颈,这是测试程序:
for (int max = 10000; max <= 10000000; max *= 10)
{
List<string> list = new List<string>();
LinkedList<string> linkedlist = new LinkedList<string>();
Queue<string> queue = new Queue<string>();
HashSet<string> hashset = new HashSet<string>();
string[] array = new string[max];
Random rand = new Random();
string value;
DateTime start = DateTime.Now;
for (int i = 0; i < max; ++i)
list.Add(rand.Next().ToString());
for (int i = 0; i < max; ++i)
value = list[i];
DateTime dtlist = DateTime.Now;
for (int i = 0; i < max; ++i)
linkedlist.AddLast(rand.Next().ToString());
var head=linkedlist.First;
for (int i = 0; i < max; ++i)
{
value = head.Value;
head = head.Next;
}
DateTime dtlinkedlist = DateTime.Now;
for (int i = 0; i < max; ++i)
queue.Enqueue(rand.Next().ToString());
for (int i = 0; i < max; ++i)
value = queue.Dequeue();
DateTime dtqueue = DateTime.Now;
for (int i = 0; i < max; ++i)
hashset.Add(rand.Next().ToString());
var ihash=hashset.GetEnumerator();
for (int i = 0; i < max; ++i)
{
value = ihash.Current;
ihash.MoveNext();
}
DateTime dthashset = DateTime.Now;
for (int i = 0; i < max; ++i)
array[i] = rand.Next().ToString();
for (int i = 0; i < max; ++i)
value = array[i];
DateTime dtarray = DateTime.Now;
Console.WriteLine("List " + list.Count + ": " + new TimeSpan(dtlist.Ticks - start.Ticks).TotalSeconds);
Console.WriteLine("LinkedList " + linkedlist.Count + ": " + new TimeSpan(dtlinkedlist.Ticks - dtlist.Ticks).TotalSeconds);
Console.WriteLine("Queue " + queue.Count + ": " + new TimeSpan(dtqueue.Ticks - dtlinkedlist.Ticks).TotalSeconds);
Console.WriteLine("HashSet " + hashset.Count + ": " + new TimeSpan(dthashset.Ticks - dtqueue.Ticks).TotalSeconds);
Console.WriteLine("Array " + array.Length + ": " + new TimeSpan(dtarray.Ticks - dthashset.Ticks).TotalSeconds);
Console.WriteLine();
}
这是我电脑上的输出:
List 10000: 0,0070058
LinkedList 10000: 0,0010009
Queue 0: 0,0020004
HashSet 10000: 0,0019973
Array 10000: 0,0040013
List 100000: 0,0139995
LinkedList 100000: 0,0270084
Queue 0: 0,0239972
HashSet 99992: 0,0320128
Array 100000: 0,0229999
List 1000000: 0,225034
LinkedList 1000000: 0,2970565
Queue 0: 0,2606011
HashSet 999767: 0,4960486
Array 1000000: 0,2189983
List 10000000: 2,3172126
LinkedList 10000000: 3,4592683
Queue 0: 3,1272267
HashSet 9976601: 6,2188591
Array 10000000: 2,3435249