Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Last active September 12, 2017 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattwarren/4c2b2e3585f8b9ad0f95a2a676c552bd to your computer and use it in GitHub Desktop.
Save mattwarren/4c2b2e3585f8b9ad0f95a2a676c552bd to your computer and use it in GitHub Desktop.
LinqOptimisation Benchmarks - Count
namespace LinqOptimisationBenchmarks
{
[Config(typeof (BenchmarkConfig))]
public class CountBenchmarks
{
private static readonly int[] items;
static CountBenchmarks()
{
items = Enumerable.Range(0, 1000).ToArray();
}
[Benchmark(Baseline = true)]
public int Iterative()
{
var counter = 0;
foreach (var item in items)
{
if (item % 10 == 0)
counter += item;
}
return counter;
}
[Benchmark]
[Shaman.Runtime.NoLinqRewrite] // Yay, this works!!
public int Linq()
{
return items.Where(i => i % 10 == 0).Count();
}
[Benchmark]
public int RoslynLinqRewrite()
{
return RoslynLinqRewrite_ProceduralLinq1(items);
}
int RoslynLinqRewrite_ProceduralLinq1(int[] _linqitems)
{
if (_linqitems == null)
throw new System.ArgumentNullException();
var _count = 0;
for (int _index = 0; _index < _linqitems.Length; _index++)
{
var _linqitem = _linqitems[_index];
if (_linqitem % 10 == 0)
{
_count++;
}
}
return _count;
}
private static readonly Func<int[], int> compiledSumQuery = Extensions.Compile<int[], int>(ps => ps.AsQueryExpr().Where(i => i % 10 == 0).Count());
[Benchmark]
[Shaman.Runtime.NoLinqRewrite]
public int LinqOptimiser()
{
return compiledSumQuery(items);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment