話說Dictionary的效率比List的高?html
爲何高呢?這個你們能夠百度下。數組
固然,我也並非徹底認同。而後後了測試,反正結果是……性能
其實在不少狀況下是根據不一樣的使用環境來選擇使用。測試
例如:List<int> 和 Dictionary<int,int>this
就拿這兩個的添加和數據遍歷或者是查找單一數據體spa
當循環遍歷查找1500次,發現list的速度下降了不少。pwa
能夠發現for循環查找的效率隨次數增長而遞減。那麼我也很好奇,爲何會這樣子捏?List一次的Find和1000次的Find會有這麼大的效率之差?初步猜想是因爲List的for循環引發。3d
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ListVsDictionary { class Program { static void Main(string[] args) { Stopwatch watch1 = new Stopwatch(); Stopwatch watch2 = new Stopwatch(); Dictionary<int, Baga> dic = new Dictionary<int, Baga>(); List<Baga> list = new List<Baga>(); Console.WriteLine("Add Function:"); watch1.Start(); for (int i = 0; i < 9000; i++) { dic.Add(i, new Baga(i, i, 8)); } Console.WriteLine("Dic: \t" + watch1.Elapsed); watch1.Stop(); watch2.Start(); for (int i = 0; i < 9000; i++) { list.Add(new Baga(i, i, 8)); } Console.WriteLine("List: \t" + watch2.Elapsed); watch2.Stop(); Console.WriteLine("For Function:"); watch1.Start(); foreach (var item in dic) { var dd = item.Value.ToString(); } Console.WriteLine("Dic: \t" + watch1.Elapsed); watch1.Stop(); watch2.Start(); foreach (var item in list) { var dd = item; } Console.WriteLine("List: \t" + watch2.Elapsed); watch2.Stop(); Console.WriteLine("Search Function:"); watch1.Start(); Baga idVal1 = new Baga(); dic.TryGetValue(99, out idVal1); Console.WriteLine("Dic: \t" + watch1.Elapsed); watch1.Stop(); watch2.Start(); list.Find(x => x.Id == 99); Console.WriteLine("List: \t" + watch2.Elapsed); watch2.Stop(); Console.WriteLine("For Search Function:"); watch1.Start(); Baga idVal = new Baga(); for (int i = 0; i < 1500; i++) { var getDic = dic.TryGetValue(999, out idVal); } Console.WriteLine("Dic: \t" + watch1.Elapsed); watch1.Stop(); watch2.Start(); for (int i = 0; i < 1500; i++) { var getList = list.Find(x => x.Id == 999); } Console.WriteLine("List: \t" + watch2.Elapsed); watch2.Stop(); } } public class Baga { public Baga() { } #region 初始化 public Baga(int id, int num, int type) { this.Id = id; this.Num = num; this.Type = type; this.Name = "asdfffffffffffffffffffffffffffffasdfffffffffffffffffff"; } #endregion public int Id { get; set; } public int Num { get; set; } public int Type { get; set; } public string Name { get; set; } public int FriendsToken { get; private set; } public int Energy { get; private set; } public int MaxEnergy { get; private set; } } }
最後,給出MSDN上的建議:code
1.若是須要很是快地添加、刪除和查找項目,並且不關心集合中項目的順序,那麼首先應該考慮使用 System.Collections.Generic.Dictionary<TKey, TValue>(或者您正在使用 .NET Framework 1.x,能夠考慮 Hashtable)。三個基本操做(添加、刪除和包含)均可快速操做,即便集合包含上百萬的項目。htm
2.若是您的使用模式不多須要刪除和大量添加,而重要的是保持集合的順序,那麼您仍然能夠選擇 List<T>。雖然查找速度可能比較慢(由於在搜索目標項目時須要遍歷基礎數組),但能夠保證集合會保持特定的順序。
3.您能夠選擇 Queue<T> 實現先進先出 (FIFO) 順序或 Stack<T> 實現後進先出 (LIFO) 順序。雖然 Queue<T> 和 Stack<T> 都支持枚舉集合中的全部項目,但前者只支持在末尾插入和從開頭刪除,然後者只支持從開頭插入和刪除。
4.若是須要在實現快速插入的同時保持順序,那麼使用新的 LinkedList<T> 集合可幫助您提升性能。與 List<T> 不一樣,LinkedList<T> 是做爲動態分配的對象鏈實現。與 List<T> 相比,在集合中間插入對象只須要更新兩個鏈接和添加新項目。從性能的角度來看,連接列表的缺點是垃圾收集器會增長其活動,由於它必須遍歷整個列表以確保沒有對象沒有被釋放。另外,因爲每一個節點相關的開銷以及每一個節點在內存中的位置等緣由,大的連接列表可能會出現性能問題。雖然將項目插入到 LinkedList<T> 的實際操做比在 List<T> 中插入要快得多,可是找到要插入新值的特定位置仍需遍歷列表並找到正確的位置。