msdn敘述:
The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedList<TKey, TValue> generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:
SortedList<TKey, TValue> uses less memory than SortedDictionary<TKey,
TValue>.
SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>.
If the list is populated all at once from sorted data, SortedList<TKey,
TValue> is faster than SortedDictionary<TKey, TValue>.
譯文:
SortedDictionary<TKey, TValue>泛型類是檢索O(log n)的二叉搜索樹,其中n是字典中的元素數。在這里,它類似於SortedList<TKey, TValue>泛型類。這兩個類有相似的對象模型,並且都有O(log n)檢索。這兩個類的不同之處在於內存的使用以及插入和刪除的速度:
SortedList<TKey, TValue>比SortedDictionary<TKey, TValue >使用更少的內存.
SortedDictionary<TKey, TValue>對於未排序的數據O(log n)具有更快的插入和刪除操作,而SortedList<TKey, TValue>的插入和刪除都是O(n)
如果列表是由已排序的數據一次填充的,那么SortedList<TKey, TValue>要比SortedDictionary<TKey, TValue>快。
兩者基本敘述:
SortedList:是一個已序的數組(基於KeyValuePair的數組)。基於鍵值排序的鍵值對數組,使用二分查找(log n)檢索key,也可根據index檢索(log 1),add和remove都是o(n)。SortedList為了保持數組的排序,它會移動位於插入的元素位置之后的所有元素(使用Array.Copy()),由於每次的插入都會重新排序,導致插入時的性能很差,因此並不推薦使用SortedList排序一個數組。
SortedDictionary: 是一個BST,基於二叉查找樹實現,使用二分查找檢索(key),add和remove都是o(log n)
兩者性能比較:
兩者實現比較:
參考:
https://stackoverflow.com/questions/935621/whats-the-difference-between-sortedlist-and-sorteddictionary
https://stackoverflow.com/questions/1376965/when-to-use-a-sortedlisttkey-tvalue-over-a-sorteddictionarytkey-tvalue