Skip to content

Instantly share code, notes, and snippets.

@main--
Last active August 29, 2015 14:19
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 main--/c8aeafe30170e28d5ec1 to your computer and use it in GitHub Desktop.
Save main--/c8aeafe30170e28d5ec1 to your computer and use it in GitHub Desktop.
ArrayList vs LinkedList
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace katzenjammer
{
class MainClass
{
public static void Main(string[] args)
{
var swag = new List<int>();
var noswag = new LinkedList<int>();
var watch = new Stopwatch();
int sz = 10000000;
watch.Start();
for (int i = 0; i < sz; i++)
swag.Add(i);
watch.Stop();
var swagInsert = watch.Elapsed;
watch.Reset();
watch.Start();
for (int i = 0; i < sz; i++)
noswag.AddLast(i);
watch.Stop();
var noswagInsert = watch.Elapsed;
watch.Reset();
watch.Start();
int swagSum = 0;
foreach (var x in swag)
swagSum += x;
watch.Stop();
var swagIter = watch.Elapsed;
watch.Reset();
watch.Start();
int noswagSum = 0;
foreach (var x in noswag)
noswagSum += x;
watch.Stop();
var noswagIter = watch.Elapsed;
Console.WriteLine("swagSum={0} noswagSum={1}", swagSum, noswagSum);
Console.WriteLine();
Console.WriteLine("swag vs noswag");
Console.WriteLine("==============");
Console.WriteLine("Insert: {0} vs {1}", swagInsert, noswagInsert);
Console.WriteLine("Iterate: {0} vs {1}", swagIter, noswagIter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment