Created
March 17, 2015 22:54
LinkedList perf test 01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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