C# 在数组末尾增加一个元素
在我的程序中,我有一堆不断增长的数组,其中新元素一个接一个地增长到数组的末尾。我发现列表是我的程序关键部分的速度瓶颈,因为与数组相比,列表的访问时间很慢 - 切换到数组可以将性能极大地提高到可接受的水平。因此,为了增加数组,我使用 Array.Resize。这很有效,因为我的实现将数组大小限制为大约 20 个元素,因此 Array.Resize 的 O(N) 性能是有限的。
但如果有一种方法可以在数组末尾增加一个元素而不必使用 Array.Resize ,那就更好了;我相信它会将旧数组复制到新大小的数组。
所以我的问题是,是否有一种更有效的方法可以在不使用 List 或 Array.Resize 的情况下将一个元素添加到数组末尾?
In my program I have a bunch of growing arrays where a new element is grown one by one to the end of the array. I identified Lists to be a speed bottleneck in a critical part of my program due to their slow access time in comparison with an array - switching to an array increased performance tremendously to an acceptable level. So to grow the array i'm using Array.Resize. This works well as my implementation restricts the array size to approximately 20 elements, so the O(N) performance of Array.Resize is bounded.
But it would be better if there was a way to just increase an array by one element at the end without having to use Array.Resize; which I believe does a copy of the old array to the newly sized array.
So my question is, is there a more efficiant method for adding one element to the end of an array without using List or Array.Resize?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
List
与数组一样具有恒定时间访问权限。对于“增长数组”,您确实应该使用List
。当您知道可能要向数组支持的结构中添加元素时,您不想一次添加一个新大小。通常,最好在数组填满时将其大小加倍来扩大数组。
A
List
has constant time access just like an array. For 'growing arrays' you really should be usingList
.When you know that you may be adding elements to an array backed structure, you don't want to add one new size at a time. Usually it is best to grow an array by doubling it's size when it fills up.
正如前面提到的,
List
就是您要寻找的内容。如果您知道列表的初始大小,则可以向 构造函数,这将提高初始分配的性能:
As has been previously mentioned,
List<T>
is what you are looking for. If you know the initial size of the list, you can supply an initial capacity to the constructor, which will increase your performance for your initial allocations:List 首先分配 4 个元素(除非您在构造它时指定了容量),然后每 4 个元素增长一次。
你为什么不用数组尝试类似的事情呢?即,将其创建为具有 4 个元素,然后当您插入第五个元素时,首先将数组增加另外 4 个元素。
List's allocate 4 elements to begin with (unless you specify a capacity when you construct it) and then grow every 4 elements.
Why don't you try a similar thing with Array? I.e. create it as having 4 elements, then when you insert the fifth element, first grow the array by another 4 elements.
无法调整数组的大小,因此获得更大数组的唯一方法是使用 Array.Resize 创建新数组。
为什么不直接创建从一开始就有 20 个元素的数组(或者您最多需要的任何容量),并使用一个变量来跟踪数组中使用了多少个元素?这样您就不必调整任何数组的大小。
There is no way to resize an array, so the only way to get a larger array is to use
Array.Resize
to create a new array.Why not just create the arrays to have 20 elements from start (or whatever capacity you need at most), and use a variable to keep track of how many elements are used in the array? That way you never have to resize any arrays.
增长数组 AFAIK 意味着分配一个新数组,现有内容被复制到新实例。我怀疑这应该比使用
List
更快...?Growing an array AFAIK means that a new array is allocated, the existing content being copied to the new instance. I doubt that this should be faster than using
List
...?以块(如 10)为单位调整数组大小并将其存储为单独的变量(例如容量)然后仅在达到容量时才调整数组大小要快得多。这就是列表的工作方式,但如果您更喜欢使用数组,那么您应该考虑以更大的块调整它们的大小,特别是如果您有大量 Array.Resize 调用
it's much faster to resize an array in chunks (like 10) and store this as a seperate variable e.g capacity and then only resize the array when the capacity is reached. This is how a list works but if you prefer to use arrays then you should look into resizing them in larger chunks especially if you have a large number of Array.Resize calls
我认为每个想要使用数组的方法都不会被优化,因为数组是静态结构,所以我认为最好使用动态结构,如 List 或其他结构。
I think that every method, that wants to use array, will not be ever optimized because an array is a static structure so I think it's better to use dynamic structures like List or others.