Skip to content

Instantly share code, notes, and snippets.

@markheath
Created May 6, 2016 20:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markheath/248293933a1cb9eb5aaca22ef9ceaf76 to your computer and use it in GitHub Desktop.
Save markheath/248293933a1cb9eb5aaca22ef9ceaf76 to your computer and use it in GitHub Desktop.
Simple PLINQ and LINQ Optimizer performance test
var listSize = 10000000;
var stopwatch = new Stopwatch();
stopwatch.Restart();
var s = Enumerable.Range(1, listSize)
.Select(n => n * 2)
.Select(n => Math.Sin((2 * Math.PI * n)/1000))
.Select(n => Math.Pow(n,2))
.Sum();
stopwatch.Stop();
Console.WriteLine("LINQ {0} items in {1}ms", listSize, stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
double sum = 0;
for (int n = 1; n <= listSize; n++)
{
var a = n * 2;
var b = Math.Sin((2 * Math.PI * a)/1000);
var c = Math.Pow(b,2);
sum += c;
}
stopwatch.Stop();
Console.WriteLine("for loop {0} items in {1}ms", listSize, stopwatch.ElapsedMilliseconds);
var q = Enumerable.Range(1, listSize).AsQueryExpr()
.Select(n => n * 2)
.Select(n => Math.Sin((2 * Math.PI * n) / 1000))
.Select(n => Math.Pow(n, 2))
.Sum();
q.Compile();
stopwatch.Restart();
var s2 = q.Run();
stopwatch.Stop();
Console.WriteLine("LinqOptimizer.CSharp {0} items in {1}ms", listSize, stopwatch.ElapsedMilliseconds);
stopwatch.Restart();
var s3 = Enumerable.Range(1, listSize).AsParallel()
.Select(n => n * 2)
.Select(n => Math.Sin((2 * Math.PI * n) / 1000))
.Select(n => Math.Pow(n, 2))
.Sum();
stopwatch.Stop();
Console.WriteLine("PLINQ {0} items in {1}ms", listSize, stopwatch.ElapsedMilliseconds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment