Created
December 8, 2016 11:36
-
-
Save aliostad/5aa25eb29f72aa0624934212fbe0fb4b to your computer and use it in GitHub Desktop.
Linq OrderBy vs Array.Sort
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
LinqVsArraySort(); | |
} | |
static void LinqVsArraySort() | |
{ | |
const int Count = 1000*1000; // 1M | |
var random = new Random(); | |
var array1 = Enumerable.Range(0, Count).Select(x => random.NextDouble()).ToArray(); | |
var array2 = Enumerable.Range(0, Count).Select(x => random.NextDouble()).ToArray(); | |
var stopwatch = Stopwatch.StartNew(); | |
Array.Sort(array1); | |
Console.WriteLine("Array Sort: " + stopwatch.Elapsed); | |
stopwatch.Restart(); | |
var doubles = array2.OrderBy(x => x).ToArray(); | |
Console.WriteLine("Linq OrderBy: " + stopwatch.Elapsed); | |
stopwatch.Restart(); | |
var copy = doubles.ToArray(); | |
Console.WriteLine("Cost of creation and copying Array: " + stopwatch.Elapsed); | |
Console.Read(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment