views:

456

answers:

7

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?

+7  A: 

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.

jjnguy
and if you need an array at the _end_ of processing .ToArray() of course.
Sky Sanders
A: 

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.

Andy Shellam
A: 

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

Thorsten Dittmar
List seems to be slower than arrays at reads. In the critical section of my code I read the array alot so it makes a huge difference.
Projectile Fish
A: 

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

JamesB
+2  A: 

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);
Richard Szalay
+1  A: 

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.

Guffa
A: 

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.

xdevel2000