Skip to content

Instantly share code, notes, and snippets.

@Metapyziks
Created April 26, 2012 13:03
Show Gist options
  • Select an option

  • Save Metapyziks/2499404 to your computer and use it in GitHub Desktop.

Select an option

Save Metapyziks/2499404 to your computer and use it in GitHub Desktop.
Adding a range to List and LinkedList speed comparison
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