I'm trying to do a simple benchmark comparing a few operations on a LinkedList and a List. The LinkedList operations are all supposed to be O(1), yet I'm getting a significant spike when doing a LinkedList insert at sample size 100. Also, the List has a big spike when doing the prepend at sample size of 100,000. Is it that the Stopwatch is just unreliable at such low tick values and spikes every now and then? I've ran this several times now, and always getting these flukes.
I've also tried creating a new Stopwatch for each of the operations, for each sample size, but not getting the expected time complexities either.
void ListInsertionComparison()
{
for (var i = 100; i <= 1000000; i*=10)
{
var stopwatch = new Stopwatch();
var random = new Random(12345);
var localList = new List<int>();
var localLinkedList = new LinkedList<int>();
var middleNode = new LinkedListNode<int>(5);
for (var j = 0; j < i; j++)
{
localList.Add(j);
var temp = localLinkedList.AddLast(j);
if (j == i / 2)
middleNode = temp;
}
stopwatch.Reset();
stopwatch.Start();
localList.Insert(0,1);
stopwatch.Stop();
Console.WriteLine($"List Prepend: {i} {stopwatch.ElapsedTicks}");
stopwatch.Reset();
stopwatch.Start();
localLinkedList.AddFirst(1);
stopwatch.Stop();
Console.WriteLine($"LinkedList Prepend: {i} {stopwatch.ElapsedTicks}");
stopwatch.Reset();
stopwatch.Start();
localList.Insert(localList.Count/2, 1);
stopwatch.Stop();
Console.WriteLine($"List Insert: {i} {stopwatch.ElapsedTicks}");
stopwatch.Reset();
stopwatch.Start();
localLinkedList.AddBefore(middleNode, 1);
stopwatch.Stop();
Console.WriteLine($"LinkedList Insert: {i} {stopwatch.ElapsedTicks}");
}
}
List Prepend: 100 160
LinkedList Prepend: 100 16
List Insert: 100 24
LinkedList Insert: 100 3854
List Prepend: 1000 7
LinkedList Prepend: 1000 2
List Insert: 1000 7
LinkedList Insert: 1000 11
List Prepend: 10000 12
LinkedList Prepend: 10000 2
List Insert: 10000 10
LinkedList Insert: 10000 2
List Prepend: 100000 272081
LinkedList Prepend: 100000 4
List Insert: 100000 575
LinkedList Insert: 100000 57
List Prepend: 1000000 8929
LinkedList Prepend: 1000000 6
List Insert: 1000000 3652
LinkedList Insert: 1000000 15