views:

80

answers:

2

A lot of the collection classes in .Net (i.e., List<T>, Dictionary<TKey, TValue>) have an overloaded constructor that lets you specify an initial capacity size. Is it a best practice to use this constructor? If so, is there a rule of them as to some kind of "Magic Number" you should use? It's one thing If I know the exact size ahead of time, but what if I don't?

+4  A: 

Is it a best practice to use this constructor?

Definitely. When juggling large quantities of data, this may make the application much more efficient because it effectively prevents repeated allocation and copying of large data batches when filling the container continuously.

To give an example using the List data structure:

In both cases, insertion actually has the same amortized constant runtime. This means that regardless of the container size, an Add operation will take a constant time. However, this is only true on average. If the container has to be resized internally because it would otherwise overflow, this one Add operation actually takes O(n), i.e. its time is proportional to the container's size.

Over the course of many added elements this won't really matter, but for large n a single insert operation can be perceivable for the user, if the UI freezes during that time.

This will never happen if you've already reserved a sufficiently large capacity from the beginning.

If so, is there a rule of them as to some kind of "Magic Number" you should use?

No. If you know the (even approximate) size, use it. If you don't, don't bother. The automatic enlargement strategy for these containers is actually pretty good and guesswork will fare much poorer in most cases (unless you have an informed guess, but then it's not really guessing at all, is it?).

Konrad Rudolph
+1  A: 

Since List<T> objects grow by the power of 2 in size, it's not really important performance-wise to specifiy an initial size, unless you know that the collection will get very big. In this case, it might make sense to initialize to collection to a large size to avoid resizing it a couple of times.

Normally, if I know exactly the size of my collection before creating it, I just create an array and return it as an IEnumerable<T>

DrJokepu
Seriously, I really wonder why did this get downvoted? How is my answer any different from Konrad's?
DrJokepu
apparently because your not Konrad =)
Micah