Skip to content

Instantly share code, notes, and snippets.

@zeldafreak
Created March 17, 2015 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeldafreak/d11ae7781f5d43206f65 to your computer and use it in GitHub Desktop.
Save zeldafreak/d11ae7781f5d43206f65 to your computer and use it in GitHub Desktop.
LinkedList perf test 01
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace LinkedListApp
{
class Program
{
private static void Main(string[] args)
{
const int count = 20000000;
for (var i = 0; i < 5; i++)
{
Console.WriteLine("Test Run #" + (i + 1));
Console.WriteLine();
Test1(count);
Console.WriteLine();
GC.Collect();
}
Console.Write("Press enter to end program...");
Console.ReadLine();
}
private static void Test1(int count = 20000000)
{
var ll = new LinkedList<int>();
var list = new List<int>();
var sw = new Stopwatch();
double t1, t2, t3, t4;
int y = 0, z = 0;
var rand = new Random();
var nodes = new LinkedListNode<int>[count];
var values = new int[count];
for (var i = 0; i < count; ++i)
{
values[i] = rand.Next();
nodes[i] = new LinkedListNode<int>(values[i]);
}
// LinkedList add
sw.Start();
LinkedListNode<int> lastNode = null;
for (int x = 0; x < count; ++x)
{
var node = nodes[x];
if (lastNode != null)
ll.AddAfter(lastNode, node);
else
ll.AddLast(node);
lastNode = node;
}
sw.Stop();
t1 = sw.Elapsed.TotalMilliseconds;
// List add
sw.Restart();
for (int x = 0; x < count; ++x) list.Add(values[x]);
sw.Stop();
t2 = sw.Elapsed.TotalMilliseconds;
// LinkedList Iterate
sw.Restart();
foreach (var i in ll) ++y;
sw.Stop();
t3 = sw.Elapsed.TotalMilliseconds;
// List Iterate
sw.Restart();
foreach (var i in list) ++z;
sw.Stop();
t4 = sw.Elapsed.TotalMilliseconds;
// Output times...
Console.WriteLine("\tLinked List Insert: " + t1 + "ms");
Console.WriteLine("\tList Insert: " + t2 + "ms");
Console.WriteLine("\tLinked List Iterate: " + t3 + "ms");
Console.WriteLine("\tList Iterate: " + t4 + "ms");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment