将iEnumerable初始化到列表/队列/堆栈中的成本是多少?

发布于 2025-02-12 14:49:21 字数 389 浏览 0 评论 0原文

我有一个可能有数百万个要素的集合。但是,在某些操作过程中,该集合可以被淘汰,然后覆盖。

我想知道使用iEnumerable创建新的数据结构的成本,例如:

IEnumerable<int> collection = /* some arbitrary collection here */

/// On average, how long will this take?
List<int> converted = new List(collection);

这将决定我是手动(即删除,Decqueue,Pop等)还是通过覆盖来进行手动审议。

我认为内部处理的方式是,不涉及将此O(1)进行复制 - 开始是入口点和元素相应地遵循的 - 但我不确定。

I have a collection that could potentially have millions of elements within it. However, during certain operations, this collection may be culled and then overwritten.

I would like to know the cost of creating a new data-structure using IEnumerable, for example:

IEnumerable<int> collection = /* some arbitrary collection here */

/// On average, how long will this take?
List<int> converted = new List(collection);

This will dictate whether I will cull manually (i.e. Remove, Dequeue, Pop, etc.) or by overwriting.

The way I imagine it is handled internally is that no copying is involved making this O(1) - where the beginning is the entry-point and elements are followed accordingly - but I'm not sure.

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

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

发布评论

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

评论(1

孤独患者 2025-02-19 14:49:22

list&lt; t&gt;的构造函数特别是

     public List(IEnumerable<T> collection)
     {
         if (collection == null)
             ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);

         if (collection is ICollection<T> c)
         {
             int count = c.Count;
             if (count == 0)
             {
                 _items = s_emptyArray;
             }
             else
             {
                 _items = new T[count];
                 c.CopyTo(_items, 0);
                 _size = count;
             }
         }
         else
         {

此调用到这里,

     public void CopyTo(T[] array, int arrayIndex)
     {
         // Delegate rest of error checking to Array.Copy.
         Array.Copy(_items, 0, array, arrayIndex, _size);
     }

这是一个非常有效的本机数组副本。

其他集合可能具有不同的实现,但是大多数收集确实对Icollection&lt; t&gt;进行了优化,因为可以计算大小。

任意iEnumerable&lt; t&gt;的大小是不可知的,并且在列举所有项目之前可能不存在。

The constructor for List<T> in particular has specific handling for ICollection<T>

     public List(IEnumerable<T> collection)
     {
         if (collection == null)
             ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);

         if (collection is ICollection<T> c)
         {
             int count = c.Count;
             if (count == 0)
             {
                 _items = s_emptyArray;
             }
             else
             {
                 _items = new T[count];
                 c.CopyTo(_items, 0);
                 _size = count;
             }
         }
         else
         {

This calls through to here

     public void CopyTo(T[] array, int arrayIndex)
     {
         // Delegate rest of error checking to Array.Copy.
         Array.Copy(_items, 0, array, arrayIndex, _size);
     }

which is a pretty efficient native array copy.

Other collections may have different implementations, but most do have optimizations for ICollection<T> because it is possible to calculate the size.

The size of an arbitrary IEnumerable<T> is unknowable, and may not exist until all items have been enumerated.

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