簡體   English   中英

添加和枚舉最快的收集類型

[英]Fastest collection type for adding and enumeration

當我不在乎重復項,訂單等時,什么是最快的集合類型?當我只想向其添加特定類型的對象,然后使用for循環遍歷集合時。 我不知道將添加多少個項目。

我目前正在使用List(Of)作為數組,這需要我知道我不知道的大小。 但是當集合增長到一百萬個對象時,這是一個瓶頸。 什么比List(Of)更快/更好?

我以前讀過一些類似的問題。 但是我可能錯了,但是我覺得哈希集的目的不對,因為集的概念並不是我要使用的。 像我說的數組要求我在添加項目之前知道大小。 字典是錯誤的目的。

我不相信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();
        }

這是我PC上的輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM