Created
April 26, 2012 13:03
-
-
Save Metapyziks/2499404 to your computer and use it in GitHub Desktop.
Adding a range to List and LinkedList speed comparison
This file contains hidden or 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 ListTest | |
| { | |
| static class Program | |
| { | |
| public static void AppendMany<T>( this LinkedList<T> collection, params T[] elements ) | |
| { | |
| foreach ( var element in elements ) | |
| { | |
| collection.AddLast( element ); | |
| } | |
| } | |
| static void Main( string[] args ) | |
| { | |
| int[] items = new int[ 65536 ]; | |
| Random rand = new Random(); | |
| for ( int i = 0; i < 65536; ++i ) | |
| items[ i ] = rand.Next(); | |
| LinkedList<int> link = new LinkedList<int>(); | |
| List<int> list = new List<int>(); | |
| Stopwatch timer = new Stopwatch(); | |
| timer.Start(); | |
| for ( int i = 0; i < 65536; ++i ) | |
| { | |
| list.AddRange( items ); | |
| list.Clear(); | |
| } | |
| timer.Stop(); | |
| Console.WriteLine( "Array list: " + timer.ElapsedMilliseconds + "ms" ); | |
| timer.Restart(); | |
| for ( int i = 0; i < 65536; ++i ) | |
| { | |
| link.AppendMany( items ); | |
| link.Clear(); | |
| } | |
| timer.Stop(); | |
| Console.WriteLine( "Linked list: " + timer.ElapsedMilliseconds + "ms" ); | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment