将iEnumerable初始化到列表/队列/堆栈中的成本是多少?
我有一个可能有数百万个要素的集合。但是,在某些操作过程中,该集合可以被淘汰,然后覆盖。
我想知道使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
list&lt; t&gt;
的构造函数特别是此调用到这里,
这是一个非常有效的本机数组副本。
其他集合可能具有不同的实现,但是大多数收集确实对
Icollection&lt; t&gt;
进行了优化,因为可以计算大小。任意
iEnumerable&lt; t&gt;
的大小是不可知的,并且在列举所有项目之前可能不存在。The constructor for
List<T>
in particular has specific handling forICollection<T>
This calls through to here
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.