Okay.
Here are the test times.
This compares loading a list with random strings VS loading an array with random strings.
The list uses ".Add".
The collections are loaded using a for loop.
Iterator count = 9999999
While the size of the array was specified during declaration,
The lists capacity was also specified before loading it.
Number of milliseconds that Array is faster than List at loading:
-6
212
10
105
155
-68
22.
Typically, full loading time of both the array and list is about 10000 MS, so the above numbers are very similar.
Personally, looking at those numbers, there is very little to choose between List and Array, with Array marginally faster.
Now, lets see what happens when we don't specify the capacity of the list and we just load it up, allowing it to resize dynamically, as it increases in size.
Number of milliseconds that list with fixed capacity is faster than List with dynamic capacity.
447
161
214
126
277
From the above results we see that if you specify the capacity of the list, performance of the list loading improves. However, the increase in performance is once again, very small.
If the full list loading procedure is about 5000MS and you can shave off about 200MS off of that, the performance improvement is about 3%.
In summary: if you were loading an array and list, the speed is as follows:
1. Array (fastest)
2. List (pre-specified capacity, using .Add)
3. List (dynamic capacity, capacity is increased dynamically when required, using .Add)
The above order also tallies with Goofball's tests.
Here's the code for the final test:
Code:
private void TestButton2_Click(object sender, EventArgs e)
{
Thread.CurrentThread.Priority = ThreadPriority.Highest;
int indexMax = 9999999;
List<string> theList = new List<string>(); //fixed capacity
theList.Capacity = indexMax;
var theArray = new string[indexMax];
List<string> theList2 = new List<string>(); //dynamic capacity
Stopwatch stp = new Stopwatch();
stp.Start();
for (var index = 0; index < indexMax; index++)
{
theList.Add(MathProcessingClass.GetRandomString(5,false));
}
stp.Stop();
var elapsedMS_loadListFixed = stp.ElapsedMilliseconds;
stp.Reset();
stp.Start();
//Random rand = new Random();
for (var index = 0; index < indexMax; index++)
{
//theArray[index] = MathProcessingClass.GetRandomString(5, false); //this was used for the array loading
theList2.Add(MathProcessingClass.GetRandomString(5, false));
}
stp.Stop();
var elapsedMS_loadListDynamic = stp.ElapsedMilliseconds;
MessageBox.Show("number of milliseconds which fixed capacity list loads faster than dynamic capacity list: " + (elapsedMS_loadListDynamic - elapsedMS_loadListFixed).ToString());
}
EDIT: I wonder if the OP is still subscribed to this thread