Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.