Last active
December 18, 2015 17:09
-
-
Save richardTowers/5816202 to your computer and use it in GitHub Desktop.
A comparison of LinkedList performance against List performance. Inserting elements at the start of the list.
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
void Main() | |
{ | |
const int maxHits = 100*1000; | |
var list = new List<Tuple<int, TimeSpan, TimeSpan>>(); | |
for(var i = 10; i < maxHits; i *= 2) | |
{ | |
var output = Test(i); | |
list.Add(Tuple.Create(i, output.Item1, output.Item2)); | |
} | |
list.Dump(); | |
} | |
// Define other methods and classes here | |
Tuple<TimeSpan, TimeSpan> Test(int hits) | |
{ | |
var stopwatch = new Stopwatch(); | |
var linkedList = new LinkedList<int>(); | |
var list = new List<int>(); | |
stopwatch.Start(); | |
for(var i = 0; i < hits; i++) | |
{ | |
linkedList.AddFirst(i); | |
} | |
stopwatch.Stop(); | |
var linkedListTime = stopwatch.Elapsed; | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
for(var i = 0; i < hits; i++) | |
{ | |
list.Insert(0, i); | |
} | |
stopwatch.Stop(); | |
var listTime = stopwatch.Elapsed; | |
return Tuple.Create(linkedListTime, listTime); | |
} |
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
10 , 00:00:00.0000025, 00:00:00.0000027 | |
20 , 00:00:00.0000019, 00:00:00.0000030 | |
40 , 00:00:00.0000022, 00:00:00.0000039 | |
80 , 00:00:00.0000025, 00:00:00.0000128 | |
160 , 00:00:00.0000041, 00:00:00.0000136 | |
320 , 00:00:00.0000120, 00:00:00.0000399 | |
640 , 00:00:00.0000310, 00:00:00.0001047 | |
1280 , 00:00:00.0000416, 00:00:00.0003399 | |
2560 , 00:00:00.0001413, 00:00:00.0012602 | |
5120 , 00:00:00.0002075, 00:00:00.0045908 | |
10240, 00:00:00.0003668, 00:00:00.0183956 | |
20480, 00:00:00.0029162, 00:00:00.0782387 | |
40960, 00:00:00.0015328, 00:00:00.3949834 | |
81920, 00:00:00.0105306, 00:00:01.6872317 |
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
const int listSize = 10 * 1000 * 1000; | |
var list = new List<int>(Enumerable.Range(0, listSize)); | |
var linkedList = new LinkedList<int>(Enumerable.Range(0, listSize)); | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
list.Reverse(); | |
stopwatch.Stop(); | |
stopwatch.Elapsed.Dump(); | |
// 00:00:00.0464891 | |
stopwatch.Reset(); | |
stopwatch.Start(); | |
linkedList.Reverse(); | |
stopwatch.Stop(); | |
stopwatch.Elapsed.Dump(); | |
// 00:00:00.0000047 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment