Skip to content

Instantly share code, notes, and snippets.

@zabulus
Created March 30, 2017 07:24
Show Gist options
  • Save zabulus/a117f02184f952356a43d0949eac639e to your computer and use it in GitHub Desktop.
Save zabulus/a117f02184f952356a43d0949eac639e to your computer and use it in GitHub Desktop.
Unrolling results
using System;
using System.Linq;
using System.Linq.Expressions;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
namespace UnrollBenchmark
{
public class Bench
{
private Consumer consumerLocal;
private Action delegateCombine;
private Action lambdaExpression;
[Setup]
public void BencSetup()
{
consumerLocal = new Consumer();
var mi = typeof(Bench).GetMethod(nameof(FuncToUnroll));
Action func = FuncToUnroll;
delegateCombine = (Action)Delegate.Combine(Enumerable.Repeat((Action)(object)func, 25).ToArray());
var expr = Expression.Call(Expression.Constant(this), mi);
var block = Expression.Block(Enumerable.Repeat(expr, 25));
lambdaExpression = Expression.Lambda<Action>(block).Compile();
}
public void FuncToUnroll()
{
consumerLocal.Consume(DateTime.Now);
}
[Benchmark(Baseline = true)]
public void UnrollBase()
{
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
FuncToUnroll();
}
[Benchmark]
public void UnrollDelegate()
{
delegateCombine();
}
[Benchmark]
public void UnrollExpression()
{
lambdaExpression();
}
}
class Program
{
static void Main(string[] args)
{
#if DEBUG
Bench b = new Bench();
b.BencSetup();
b.FuncToUnroll();
b.UnrollBase();
b.UnrollDelegate();
b.UnrollExpression();
#endif
BenchmarkRunner.Run<Bench>();
}
}
}
BenchmarkDotNet=v0.10.3.0, OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Core(TM) i7-6700 CPU 3.40GHz, ProcessorCount=8
Frequency=3328132 Hz, Resolution=300.4689 ns, Timer=TSC
  [Host]     : Clr 4.0.30319.42000, 32bit LegacyJIT-v4.6.1586.0
  DefaultJob : Clr 4.0.30319.42000, 32bit LegacyJIT-v4.6.1586.0

Method Mean StdDev Scaled Scaled-StdDev
UnrollBase 16.9955 us 0.0294 us 1.00 0.00
UnrollDelegate 17.0056 us 0.0194 us 1.00 0.00
UnrollExpression 17.1997 us 0.0130 us 1.01 0.00
@zabulus
Copy link
Author

zabulus commented Mar 31, 2017

BenchmarkDotNet=v0.10.3.20170330-develop, OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Core(TM) i7-6500U CPU 2.50GHz, ProcessorCount=4
Frequency=2531245 Hz, Resolution=395.0625 ns, Timer=TSC
  [Host]     : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0
  AcerAspire : .NET Native 4.6.24903.00, 64bit RyuJIT
  Clr        : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0

Method Job Runtime Mean StdDev Scaled Scaled-StdDev
UnrollBase AcerAspire AcerAspire 8.7088 us 0.3321 us 1.00 0.00
UnrollDelegate AcerAspire AcerAspire 8.8792 us 0.1923 us 1.02 0.04
UnrollExpression AcerAspire AcerAspire 13.4516 us 0.6192 us 1.55 0.09
UnrollBase Clr Clr 6.9332 us 0.0914 us 1.00 0.00
UnrollDelegate Clr Clr 6.8973 us 0.0291 us 0.99 0.01
UnrollExpression Clr Clr 7.1989 us 0.0383 us 1.04 0.01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment