.NET 集合类的渐近复杂度

发布于 2024-07-19 21:43:48 字数 298 浏览 5 评论 0 原文

是否有关于 .NET 集合类(DictionaryList 等方法的渐近复杂性(big-O 和其他)的资源...)?

我知道 C5 库的文档包含一些有关它的信息(示例),但我也对标准 .NET 集合感兴趣...(PowerCollections 的信息也很好)。

Are there any resources about the asymptotic complexity (big-O and the rest) of methods of .NET collection classes (Dictionary<K,V>, List<T> etc...)?

I know that the C5 library's documentation includes some information about it (example), but I'm interested in standard .NET collections too... (and PowerCollections' information would also be nice).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

看轻我的陪伴 2024-07-26 21:43:48

本页总结了各种集合类型的一些时间复杂度Java,尽管它们对于 .NET 应该完全相同。

我从该页面获取了表格,并针对 .NET 框架更改/扩展了它们。
另请参阅 SortedDictionarySortedList,详细说明了各种操作所需的时间复杂度。


搜索

Type of Search/Collection Types           Complexity  Comments
Linear search Array/ArrayList/LinkedList  O(N)        Unsorted data.
Binary search sorted Array/ArrayList/     O(log N)    Requires sorted data.
Search Hashtable/Dictionary<T>            O(1)        Uses hash function.
Binary search SortedDictionary/SortedKey  O(log N)    Sorting is automated.

、检索和插入、

Operation         Array/ArrayList  LinkedList  SortedDictionary  SortedList
Access back       O(1)             O(1)        O(log N)          O(log N)
Access front      O(1)             O(1)        N.A.              N.A.
Access middle     O(1)             O(N)        N.A.              N.A.
Insert at back    O(1)             O(1)        O(log N)          O(N)
Insert at front   O(N)             O(1)        N.A.              N.A.
Insert in middle  O(N)             O(1)        N.A.              N.A.

删除应该与关联集合的插入具有相同的复杂性。

SortedList 对于插入和检索有一些显着的特性。

插入(添加方法):

此方法的运算时间复杂度为 O(n)
未排序的数据,其中 n 是计数。 这是
一个 O(log n) 操作,如果新的
元素被添加到末尾
列表。 如果插入导致调整大小,
该操作是 O(n)。

检索(项目属性):

检索该属性的值
是一个 O(log n) 运算,其中 n 是
数数。 设置属性是
O(log n) 操作,如果密钥是
已经在 SortedList<(Of <(TKey,
T值>)>)。 如果密钥不在
列表,设置属性是 O(n)
对未排序数据的操作,或 O(log
n) 如果新元素添加到
列表末尾。 如果插入导致
调整大小,操作是O(n)。

请注意,就所有操作的复杂性而言,ArrayListList 等效。


This page summarises some of the time comlplexities for various collection types with Java, though they should be exactly the same for .NET.

I've taken the tables from that page and altered/expanded them for the .NET framework.
See also the MSDN pages for SortedDictionary and SortedList, which detail the time complexities required for various operations.


Searching

Type of Search/Collection Types           Complexity  Comments
Linear search Array/ArrayList/LinkedList  O(N)        Unsorted data.
Binary search sorted Array/ArrayList/     O(log N)    Requires sorted data.
Search Hashtable/Dictionary<T>            O(1)        Uses hash function.
Binary search SortedDictionary/SortedKey  O(log N)    Sorting is automated.

Retrieval and Insertion

Operation         Array/ArrayList  LinkedList  SortedDictionary  SortedList
Access back       O(1)             O(1)        O(log N)          O(log N)
Access front      O(1)             O(1)        N.A.              N.A.
Access middle     O(1)             O(N)        N.A.              N.A.
Insert at back    O(1)             O(1)        O(log N)          O(N)
Insert at front   O(N)             O(1)        N.A.              N.A.
Insert in middle  O(N)             O(1)        N.A.              N.A.

Deletion should have the same complexity as insertion for the associated collection.

SortedList has a few notable peculiarities for insertion and retrieval.

Insertion (Add method):

This method is an O(n) operation for
unsorted data, where n is Count. It is
an O(log n) operation if the new
element is added at the end of the
list. If insertion causes a resize,
the operation is O(n).

Retrieval (Item property):

Retrieving the value of this property
is an O(log n) operation, where n is
Count. Setting the property is an
O(log n) operation if the key is
already in the SortedList<(Of <(TKey,
TValue>)>). If the key is not in the
list, setting the property is an O(n)
operation for unsorted data, or O(log
n) if the new element is added at the
end of the list. If insertion causes a
resize, the operation is O(n).

Note that ArrayList is equivalent to List<T> in terms of the complexity of all operations.


九局 2024-07-26 21:43:48

MSDN 列出了这些:

等。例如:

SortedList(TKey, TValue) 泛型
class 是一个二叉搜索树
O(log n) 检索,其中 n 是
字典中的元素数量。
在这一点上,它类似于
SortedDictionary(TKey, TValue) 泛型
班级。 两个类有相似之处
对象模型,并且都有 O(log n)
恢复。 两个班级在哪里
不同之处在于内存使用和速度
插入和删除:

SortedList(TKey, TValue) 使用较少
内存比 SortedDictionary(TKey,
T值)。

SortedDictionary(TKey, TValue) 有
更快的插入和移除
对未排序数据的操作,O(log n)
与 O(n) 相反
排序列表(TKey,TValue)。

如果列表是一次性填充的
从排序数据中,SortedList(TKey,
TValue)比
SortedDictionary(TKey, TValue)。

MSDN Lists these:

etc. For example:

The SortedList(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
SortedDictionary(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).

慕巷 2024-07-26 21:43:48

本页介绍了一些主要优点和优点的简短说明。 大多数.NET集合的缺点:

http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx

<表类=“s-表”>
<标题>

集合
排序
连续存储
直接访问
查找效率
操纵效率
注释


<正文>

字典
无序

通过按键
密钥:O(1)
O(1)
最适合高性能查找。

排序字典
已排序

通过按键
键:O(log n)
O(log n)
妥协字典速度和排序,使用二叉搜索树。

排序列表
已排序

通过按键
键:O(log n)
O(n)
与 SortedDictionary 非常相似,不同之处在于树是在数组中实现的,因此对预加载数据的查找速度更快,但加载速度较慢。

列表
用户可以精确控制元素排序

通过索引
索引:O(1)
值:O(n)
O(n)
最适合需要直接访问且无需排序的较小列表。

链接列表
用户可以精确控制元素排序


值:O(n)
O(1)
最适合在中间插入/删除很常见且不需要直接访问的列表。

哈希集
无序

通过按键
密钥:O(1)
O(1)
唯一的无序集合,就像字典一样,除了键和值是同一个对象。

排序集
已排序

通过按键
键:O(log n)
O(log n)
唯一的排序集合,类似于 SortedDictionary,只不过键和值是同一个对象。

堆栈
后进先出

仅顶部
顶部:O(1)
O(1)*
与 List 基本相同,只是仅按照 LIFO 进行处理

队列
先进先出

仅前面
前面:O(1)
O(1)
与 List 基本相同,只是仅按照 FIFO 进行处理

This page presents short notes about some key pros & cons for most .NET Collections :

http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx

Collection Ordering Contiguous Storage Direct Access Lookup Efficiency Manipulate Efficiency Notes
Dictionary Unordered Yes Via Key Key: O(1) O(1) Best for high performance lookups.
SortedDictionary Sorted No Via Key Key: O(log n) O(log n) Compromise of Dictionary speed and ordering, uses binary search tree.
SortedList Sorted Yes Via Key Key: O(log n) O(n) Very similar to SortedDictionary, except tree is implemented in an array, so has faster lookup on preloaded data, but slower loads.
List User has precise control over element ordering Yes Via Index Index: O(1)
Value: O(n)
O(n) Best for smaller lists where direct access required and no sorting.
LinkedList User has precise control over element ordering No No Value: O(n) O(1) Best for lists where inserting/deleting in middle is common and no direct access required.
HashSet Unordered Yes Via Key Key: O(1) O(1) Unique unordered collection, like a Dictionary except key and value are same object.
SortedSet Sorted No Via Key Key: O(log n) O(log n) Unique sorted collection, like SortedDictionary except key and value are same object.
Stack LIFO Yes Only Top Top: O(1) O(1)* Essentially same as List except only process as LIFO
Queue FIFO Yes Only Front Front: O(1) O(1) Essentially same as List except only process as FIFO
偷得浮生 2024-07-26 21:43:48

我一般不知道(刚刚发布的另一个答案可能准确地给出了您所追求的内容) - 但您当然可以使用 ILSpy 反映这个方法和其他方法(对于 FSharp 代码有点尴尬,是的),这最终会产生此函数与 C# 相同:

internal static a maximumElementAux<a>(SetTree<a> s, a n)
{
  while (true)
  {
    SetTree<a> setTree = s;
    if (setTree is SetTree<a>.SetOne)
    {
      break;
    }
    if (setTree == null)
    {
      return n;
    }
    SetTree<a>.SetNode setNode = (SetTree<a>.SetNode)s;
    SetTree<a> arg_23_0 = setNode.item3;
    n = setNode.item1;
    s = arg_23_0;
  }
  return ((SetTree<a>.SetOne)s).item;
  return n;
}

好吧,这不完全是 C# 术语中的“正确”代码 - 但 while(true) 循环的存在意味着它至少不会是 O(1); 至于它到底是什么......好吧,我的头太痛了,无法找出答案:)

I don't know in general (the other answer just posted perhaps gives you exactly what you're after) - but you can reflect this and other methods of course using ILSpy (a little awkward with FSharp code, true) and this eventually yields this function as C#:

internal static a maximumElementAux<a>(SetTree<a> s, a n)
{
  while (true)
  {
    SetTree<a> setTree = s;
    if (setTree is SetTree<a>.SetOne)
    {
      break;
    }
    if (setTree == null)
    {
      return n;
    }
    SetTree<a>.SetNode setNode = (SetTree<a>.SetNode)s;
    SetTree<a> arg_23_0 = setNode.item3;
    n = setNode.item1;
    s = arg_23_0;
  }
  return ((SetTree<a>.SetOne)s).item;
  return n;
}

Okay so this is not exactly 'proper' code in C# terms - but the presence of the while(true) loop implies it can't be O(1) at least; as for what it actually is... well, my head hurts too much to find out :)

友欢 2024-07-26 21:43:48

不存在“集合类的复杂性”这样的事情。 相反,对这些集合的不同操作具有不同的复杂性。 例如,将元素添加到 Dictionary...

...接近 O(1) 操作。 如果必须增加容量以容纳新元素,则此方法将变为 O(n) 操作,其中 nCount

而从 Dictionary 检索元素...

...接近 O(1) 运算。

There's no such thing as "complexity of collection classes". Rather, different operations on these collections have different complexities. For instance, adding an element to a Dictionary<K, V>...

...approaches an O(1) operation. If the capacity must be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

Whereas retrieving an element from a Dictionary<K, V>...

...approaches an O(1) operation.

北方的巷 2024-07-26 21:43:48

文档说它是建立在二叉树上的,并且没有提到跟踪最大元素。 如果文档正确,则意味着它应该是 O(log n)。 集合文档中曾经至少存在一个错误(将数组支持的数据结构称为二叉搜索树),但已得到纠正。

The documentation says it is build on a binary tree, and does not mention tracking the maximum element. If the documentation is correct, that means it should be O( log n). There used to be at least one mistake in the collections documentation (referring to an array-backed data structure as a binary search tree), but that has been corrected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文