简体   繁体   中英

C# Increasing an array by one element at the end

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?

A List has constant time access just like an array. For 'growing arrays' you really should be using List .

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.

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<int> values = new List<int>(5);

values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
values.Add(5);

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.

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? Ie create it as having 4 elements, then when you insert the fifth element, first grow the array by another 4 elements.

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 ...?

it's much faster to resize an array in chunks (like 10) and store this as a seperate variable eg 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或其他动态结构。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM