Created
January 20, 2010 14:08
-
-
Save JeffreyZhao/281857 to your computer and use it in GitHub Desktop.
This file contains 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
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