Is there a best algorithm for defining the capacity of a C# list in the constructor, if the general distribution of eventual sizes is known?
As a concrete example, if the numbers of values to be placed in each list has a mean of 500, and a standard deviation of 50, with approximately a normal distribution what is the best initial capacity for the list in terms of memory consumption?
I've done a little research and it seems that there is a "right" answer to this question.
First of all I agree that this can be premature optimisation, so profiling before deciding to switch is essential.
The graph above was generated in excel, using a normal distribution, and testing the space overused by various initial list capacities, using 10,000 samples and a mean of 10,000. As you can see it has several interesting features.
Caveat: YMMV with other distributions, means etc.