更重要的是有效的:列表< T>。新增()或System.Array.Resize()? [英] What is more efficient: List<T>.Add() or System.Array.Resize()?

查看:358
本文介绍了更重要的是有效的:列表< T>。新增()或System.Array.Resize()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定什么时候它更有效地列表< T>。新增()与使用 Array.Resize()方法。

I'm trying to determine when it's more efficient to List<T>.Add() versus using the Array.Resize() method.

有关Array.Resize的文档说,这使得整个数组的副本,并将其放入一个新的对象。旧的对象将不得不被丢弃。哪里这个老对象驻留?在堆栈或堆?

The documentation for Array.Resize says it makes a copy of the entire array, and places it into a new object. The old object would have to be discarded. Where does this old object reside? On the stack or the heap?

我不知道List.Add()是如何工作的。

I don't know how List.Add() works.

有谁知道怎么List.Add方法比较静态Array.Resize方法?

Does anyone know how the List.Add method compares to the static Array.Resize method?

我感兴趣的内存使用(和清理),什么是300值类型越好,对20000值类型。

I'm interested in memory usage (and cleanup), and what is better for 300 value types, versus 20,000 value types.

有关它的价值,我打算对.NET的嵌入式风格中的一种运行此code。潜在的 .NET摆弄小玩意

For what it's worth, I'm planning on running this code on one of the embedded flavors of .NET. Potentially the .NET Gadgeteer

推荐答案

您应该使用列表&LT; T&GT;

使用 Array.Resize 将迫使你展开数组分别每次添加项目,使您的code的的慢。 (因为数组不能有余力)

Using Array.Resize will force you to expand the array separately each time you add an item, making your code much slower. (since arrays cannot have spare capacity)

A 列表&LT; T&GT; 由数组支持,但认为把物品放入余力结果
它所需要做的添加项目是数组中设置元素,并增加其内部的尺寸计数器。结果
当数组已满,该列表将增加一倍的容量,从而可以轻松地再次添加未来的项目。

A List<T> is backed by an array, but holds spare capacity to put items into.
All it needs to do to add an item is to set an element in the array and increase its internal size counter.
When the array gets full, the list will double its capacity, allowing future items to be added effortlessly again.

这篇关于更重要的是有效的:列表&LT; T&GT;。新增()或System.Array.Resize()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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