SortedList 和 SortedDictionary 有什么区别? [英] What's the difference between SortedList and SortedDictionary?

查看:21
本文介绍了SortedList 和 SortedDictionary 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<代码>SortedListSortedDictionary?在某些情况下,您会专门使用一种而不是另一种吗?

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?

推荐答案

是的 - 它们的性能特征显着不同.将它们称为 SortedListSortedTree 可能会更好,因为这更接近于实现.

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 文档(SortedListSortedDictionary) 有关不同情况下不同操作的性能的详细信息.这是一个很好的总结(来自 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 泛型类是一个二叉搜索树O(log n) 检索,其中 n 是字典中的元素数.在这方面,它类似于SortedList 通用班级.两个班级有相似之处对象模型,并且都有 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 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 使用较少内存比 SortedDictionary.

SortedDictionary 有更快的插入和移除未排序数据的操作,O(log n)与 O(n) 相反SortedList.

SortedDictionary<TKey, TValue> has faster insertion and removal operations for unsorted data, O(log n) as opposed to O(n) for SortedList<TKey, TValue>.

如果列表一次全部填充来自排序的数据,SortedListSortedDictionary.

If the list is populated all at once from sorted data, SortedList<TKey, TValue> is faster than SortedDictionary<TKey, TValue>.

(SortedList 实际上维护一个有序数组,而不是使用树.它仍然使用二分查找来查找元素.)

(SortedList actually maintains a sorted array, rather than using a tree. It still uses binary search to find elements.)

这篇关于SortedList 和 SortedDictionary 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆