Skip to content

Instantly share code, notes, and snippets.

@aliostad
Created December 8, 2016 11:36
Show Gist options
  • Save aliostad/5aa25eb29f72aa0624934212fbe0fb4b to your computer and use it in GitHub Desktop.
Save aliostad/5aa25eb29f72aa0624934212fbe0fb4b to your computer and use it in GitHub Desktop.
Linq OrderBy vs Array.Sort
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