I built off of Pattermiester's performance tests. When I think of insert, I think of an array, and in order to add to an array you have to specify the index you want to Insert at. Doing an Add(0, object) seems counter intuitive. Also, when you do performance tests in always create a throw away run in first. Sometimes it takes longer than it actually should.
Results
2.282 - Throw Away Run
2.6847 - List Insert By Index without Capacity
10544.9766 - List Insert At Index 0 without Capacity
1.8426 - List Add without Capacity
2.0835 - List Insert By Index with Capacity
1.4952 - List Add with Capacity
9323.699 - List Insert at Index 0 with Capacity
Code
var size = 200000;
//First test sometimes has a bad count
List<object> SomeList = new List<object>();
Stopwatch sw = new Stopwatch();
sw.Start();
for (var i = 0; i < size; i++)
SomeList.Insert(i, String.Empty);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds + " - Throw Away Run");
sw.Reset();
SomeList = new List<object>();
sw = new Stopwatch();
sw.Start();
for (var i = 0; i < size ; i++)
SomeList.Insert(i, String.Empty);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds+" - List Insert By Index without Capacity");
sw.Reset();
SomeList = new List<object>();
sw = new Stopwatch();
sw.Start();
for (var i = 0; i < size; i++)
SomeList.Insert(0, String.Empty);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds + " - List Insert At Index 0 without Capacity");
sw.Reset();
SomeList = new List<object>();
sw.Start();
for (var i = 0; i < size; i++)
SomeList.Add(String.Empty);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds+" - List Add without Capacity");
sw.Reset();
SomeList = new List<object>(size);
sw = new Stopwatch();
sw.Start();
for (var i = 0; i < size; i++)
SomeList.Insert(i, String.Empty);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds+" - List Insert By Index with Capacity");
sw.Reset();
SomeList = new List<object>(size);
sw.Start();
for (var i = 0; i < size; i++)
SomeList.Add(String.Empty);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds+" - List Add with Capacity");
sw.Reset();
SomeList = new List<object>(size);
sw = new Stopwatch();
sw.Start();
for (var i = 0; i < size; i++)
SomeList.Insert(0, String.Empty);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalMilliseconds + " - List Insert at Index 0 with Capacity");
So it appears that the Add is a little more performant than than insert. However, if you are using an Insert(0, ""), you are going to be killing performance that is for sure. If you want best performance, than use capacity.
List<T>
is an auto-resizing array wrapper. It is not a linked list. – TheEvilPenguin Sep 3 '13 at 8:21