Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Created January 20, 2010 14:08
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 JeffreyZhao/281857 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/281857 to your computer and use it in GitHub Desktop.
namespace SimpleConsole
{
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
var random = new Random(DateTime.Now.Millisecond);
var array = Enumerable.Repeat(0, 1000 * 500).Select(_ => random.Next()).ToArray();
CodeTimer.Initialize();
CodeTimer.Time("SortWithDefaultComparer", 100,
() => SortWithDefaultComparer(CloneArray(array)));
CodeTimer.Time("SortWithCustomComparer", 100,
() => SortWithCustomComparer(CloneArray(array)));
CodeTimer.Time("SortWithDelegate", 100,
() => SortWithDelegate(CloneArray(array)));
CodeTimer.Time("SortWithLinq", 100,
() => SortWithLinq(CloneArray(array)));
Console.ReadLine();
}
static void SortWithDefaultComparer(int[] array)
{
Array.Sort(array, Comparer<int>.Default);
}
static void SortWithCustomComparer(int[] array)
{
Array.Sort(array, new Int32Comparer());
}
static void SortWithDelegate(int[] array)
{
Array.Sort(array, (x, y) => x - y);
}
static void SortWithLinq(int[] array)
{
var sorted =
(from i in array
orderby i
select i).ToList();
}
private class Int32Comparer : IComparer<int>
{
#region IComparer<int> Members
public int Compare(int x, int y)
{
return x - y;
}
#endregion
}
static T[] CloneArray<T>(T[] source)
{
var dest = new T[source.Length];
Array.Copy(source, dest, source.Length);
return dest;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment