什么是排序列表和SortedDictionary之间的区别? [英] What's the difference between SortedList and SortedDictionary?
问题描述
有没有之间的<一个真正实用的差异href="https://msdn.microsoft.com/en-us/library/ms132319(v=vs.110).aspx"><$c$c>SortedList<TKey,TValue>$c$c>和<一href="https://msdn.microsoft.com/en-us/library/f7fta44c(v=vs.110).aspx"><$c$c>SortedDictionary<TKey,TValue>$c$c>?是否有任何情况下,你会专门使用一个而不是其他?
Is there any real practical difference between a SortedList<TKey,TValue>
and a SortedDictionary<TKey,TValue>
? Are there any circumstances where you would specifically use one and not the other?
推荐答案
是 - 它们的性能特点显著不同。或许,这将是更好地给他们打电话排序列表
和 SortedTree
因为这反映了实现更加紧密。
Yes - their performance characteristics differ significantly. It would probably be better to call them SortedList
and SortedTree
as that reflects the implementation more closely.
看MSDN文档为他们每个人( 排序列表
, SortedDictionary
一>),用于在不同situtations不同的操作的性能的详细信息。这里是一个很好的总结(从 SortedDictionary
文档):
Look at the MSDN docs for each of them (SortedList
, SortedDictionary
) for details of the performance for different operations in different situtations. Here's a nice summary (from the SortedDictionary
docs):
在 SortedDictionary&LT; TKEY的,TValue&GT;
通用
类是用二进制搜索树
O(log n)的检索,其中n是
在字典中的元素数目。
在此,它是类似于
排序列表&LT; TKEY的,TValue&GT;
通用
类。这两个类具有相似的
对象模型,并且都为O(log n)的
检索。其中,这两个类
不同之处在于内存使用和速度
插入和删除:
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 theSortedList<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:
-
排序列表&LT; TKEY的,TValue&GT;
使用较少 内存比SortedDictionary&LT; TKEY的, TValue&GT;
SortedList<TKey, TValue>
uses less memory thanSortedDictionary<TKey, TValue>
.
SortedDictionary&LT; TKEY的,TValue&GT;
有
更快的插入和移除
对于未排序的数据操作,O(log n)的
而不是为O(n),用于
排序列表&LT; TKEY的,TValue&GT;
SortedDictionary<TKey, TValue>
has
faster insertion and removal
operations for unsorted data, O(log n)
as opposed to O(n) for
SortedList<TKey, TValue>
.
如果该列表中填充一次全部
从排序的数据,排序列表&LT; TKEY的,
TValue&GT;
比快
SortedDictionary&LT; TKEY的,TValue&GT;
If the list is populated all at once
from sorted data, SortedList<TKey,
TValue>
is faster than
SortedDictionary<TKey, TValue>
.
(排序列表
其实维护一个有序数组,而不是使用一棵树,它仍然使用二进制搜索找到的元素。)
(SortedList
actually maintains a sorted array, rather than using a tree. It still uses binary search to find elements.)
这篇关于什么是排序列表和SortedDictionary之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!