Skip to content

Instantly share code, notes, and snippets.

@ig-sinicyn
Created March 22, 2016 11:54
Show Gist options
  • Save ig-sinicyn/4ea179c4549462cbe493 to your computer and use it in GitHub Desktop.
Save ig-sinicyn/4ea179c4549462cbe493 to your computer and use it in GitHub Desktop.

// * Summary * BenchmarkDotNet-Dev=v0.9.3.0+ OS=Microsoft Windows NT 6.2.9200.0 Processor=Intel(R) Core(TM) i5-2550K CPU @ 3.40GHz, ProcessorCount=4 Frequency=3312788 ticks, Resolution=301.8605 ns HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE

Type=CompareCallsBenchmark Mode=Throughput TargetCount=10

                       Method |      Median |    StdDev | Scaled |

--------------------------------- |------------ |---------- |------- | M_00_Raw | 2.8666 ms | 0.0306 ms | 1.00 | M_01_Call | 2.8746 ms | 0.0482 ms | 1.00 | M_02_GenericCall | 2.8301 ms | 0.0359 ms | 0.99 | M_03_InstanceCall | 2.8196 ms | 0.0149 ms | 0.98 | M_04_InstanceGenericCall | 2.8345 ms | 0.0445 ms | 0.99 | M_05_CallNoInline | 17.2215 ms | 0.0739 ms | 6.01 | M_06_InstanceCallNoInline | 17.2418 ms | 0.1615 ms | 6.01 | M_07_InstanceVirtualCall | 20.1605 ms | 0.1394 ms | 7.03 | M_08_DerivedVirtualCall | 19.7496 ms | 0.0898 ms | 6.89 | M_09_InterfaceCall | 25.6127 ms | 0.4918 ms | 8.93 | M_10_DerivedInterfaceCall | 25.7749 ms | 0.3940 ms | 8.99 | M_11_GenericInterfaceCall | 25.3831 ms | 0.2806 ms | 8.85 | M_12_DerivedGenericInterfaceCall | 25.5337 ms | 0.2779 ms | 8.91 | M_13_InterfaceGenericCall | 100.6017 ms | 1.0055 ms | 35.09 | M_14_DerivedInterfaceGenericCall | 100.9340 ms | 2.5098 ms | 35.21 | M_15_LambdaCached | 25.3776 ms | 0.1767 ms | 8.85 | M_16_LambdaNew | 31.1509 ms | 0.2205 ms | 10.87 | M_17_LambdaClosure | 31.1710 ms | 0.3057 ms | 10.87 | M_18_LambdaClosureLocal | 108.6519 ms | 0.3742 ms | 37.90 | M_19_FuncCached | 30.9500 ms | 0.1305 ms | 10.80 | M_20_FuncCachedInstance | 22.6820 ms | 0.2360 ms | 7.91 | M_21_FuncNew | 82.5950 ms | 1.1190 ms | 28.81 |

// ***** BenchmarkRunner: End *****

BenchmarkRunner, elapsed: 00:05:44.9721231

RunInline, elapsed: 00:00:00.7725683

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using System;
using System.Linq;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace ConsoleApplication3
{
/// <summary>
/// Use this to run slower but proven-to-be-accurate perf tests
/// </summary>
public class TestProofConfig : ManualConfig
{
/// <summary>
/// Instance of the config
/// </summary>
public static readonly IConfig Instance = new TestProofConfig();
/// <summary>
/// Constructor
/// </summary>
public TestProofConfig()
{
Add(new Job
{
TargetCount = 10
});
}
}
class Program
{
static void Main(string[] args)
{
var sw = Stopwatch.StartNew();
CompareCallsBenchmark.CompareCalls();
Console.WriteLine();
Console.WriteLine("BenchmarkRunner, elapsed: " + sw.Elapsed);
sw.Restart();
CompareCallsBenchmark.RunInline();
Console.WriteLine();
Console.WriteLine("RunInline, elapsed: " + sw.Elapsed);
Console.ReadKey();
}
}
[Config(typeof(TestProofConfig))]
public class CompareCallsBenchmark : CompareCallsBenchmark.ICompareCalls, CompareCallsBenchmark.ICompareCalls<int>
{
public static void RunInline()
{
var methods =
(from m in typeof(CompareCallsBenchmark).GetMethods()
where !m.IsStatic && Attribute.IsDefined(m, typeof(BenchmarkAttribute))
select (Func<CompareCallsBenchmark, int>)Delegate.CreateDelegate(typeof(Func<CompareCallsBenchmark, int>), m))
.ToArray();
var b = new CompareCallsBenchmark();
foreach (var method in methods)
{
method(b);
}
}
public static void CompareCalls()
{
BenchmarkRunner.Run<CompareCallsBenchmark>();
}
#region CompetitionMethods
interface ICompareCalls
{
int CallInterface(int a);
int CallInterface<T>(int a);
}
interface ICompareCalls<T>
{
T CallInterface(T a);
}
class CompareCallsDerived : CompareCallsBenchmark
{
public override int CallVirtual(int a)
{
return a + 1;
}
public override int CallInterface(int a)
{
return a + 1;
}
public override int CallInterface<T>(int a)
{
return a + 1;
}
}
private static int Call(int a)
{
return a + 1;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int CallNoInline(int a)
{
return a + 1;
}
private static int Call<T>(int a)
{
return a + 1;
}
private int CallInst(int a)
{
return a + 1;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private int CallInstNoInline(int a)
{
return a + 1;
}
private int CallInst<T>(int a)
{
return a + 1;
}
public virtual int CallVirtual(int a)
{
return a + 1;
}
public virtual int CallInterface(int a)
{
return a + 1;
}
public virtual int CallInterface<T>(int a)
{
return a + 1;
}
#endregion
const int Count = 10 * 1000 * 1000;
[Benchmark(Baseline = true)]
public int M_00_Raw()
{
int a = 0;
for (int i = 0; i < Count; i++) a = a + 1;
return Count;
}
[Benchmark]
public int M_01_Call()
{
int a = 0;
for (int i = 0; i < Count; i++) a = Call(a);
return Count;
}
[Benchmark]
public int M_02_GenericCall()
{
int a = 0;
for (int i = 0; i < Count; i++) a = Call<object>(a);
return Count;
}
[Benchmark]
public int M_03_InstanceCall()
{
int a = 0;
var p = new CompareCallsBenchmark();
for (int i = 0; i < Count; i++) a = p.CallInst(a);
return Count;
}
[Benchmark]
public int M_04_InstanceGenericCall()
{
int a = 0;
var p = new CompareCallsBenchmark();
for (int i = 0; i < Count; i++) a = p.CallInst<object>(a);
return Count;
}
[Benchmark]
public int M_05_CallNoInline()
{
int a = 0;
for (int i = 0; i < Count; i++) a = CallNoInline(a);
return Count;
}
[Benchmark]
public int M_06_InstanceCallNoInline()
{
int a = 0;
var p = new CompareCallsBenchmark();
for (int i = 0; i < Count; i++) a = p.CallInstNoInline(a);
return Count;
}
[Benchmark]
public int M_07_InstanceVirtualCall()
{
int a = 0;
var p = new CompareCallsBenchmark();
for (int i = 0; i < Count; i++) a = p.CallVirtual(a);
return Count;
}
[Benchmark]
public int M_08_DerivedVirtualCall()
{
int a = 0;
var p = new CompareCallsDerived();
for (int i = 0; i < Count; i++) a = p.CallVirtual(a);
return Count;
}
[Benchmark]
public int M_09_InterfaceCall()
{
int a = 0;
ICompareCalls p = new CompareCallsBenchmark();
for (int i = 0; i < Count; i++) a = p.CallInterface(a);
return Count;
}
[Benchmark]
public int M_10_DerivedInterfaceCall()
{
int a = 0;
ICompareCalls p = new CompareCallsDerived();
for (int i = 0; i < Count; i++) a = p.CallInterface(a);
return Count;
}
[Benchmark]
public int M_11_GenericInterfaceCall()
{
int a = 0;
ICompareCalls<int> p = new CompareCallsBenchmark();
for (int i = 0; i < Count; i++) a = p.CallInterface(a);
return Count;
}
[Benchmark]
public int M_12_DerivedGenericInterfaceCall()
{
int a = 0;
ICompareCalls<int> p = new CompareCallsDerived();
for (int i = 0; i < Count; i++) a = p.CallInterface(a);
return Count;
}
[Benchmark]
public int M_13_InterfaceGenericCall()
{
int a = 0;
ICompareCalls p = new CompareCallsBenchmark();
for (int i = 0; i < Count; i++) a = p.CallInterface<object>(a);
return Count;
}
[Benchmark]
public int M_14_DerivedInterfaceGenericCall()
{
int a = 0;
ICompareCalls p = new CompareCallsDerived();
for (int i = 0; i < Count; i++) a = p.CallInterface<object>(a);
return Count;
}
[Benchmark]
public int M_15_LambdaCached()
{
int a1 = 0;
Func<int, int> x = a => a + 1;
for (int i = 0; i < Count; i++) a1 = x(a1);
return Count;
}
[Benchmark]
public int M_16_LambdaNew()
{
int a1 = 0;
for (int i = 0; i < Count; i++)
{
Func<int, int> x = a => a + 1;
a1 = x(a1);
};
return Count;
}
[Benchmark]
public int M_17_LambdaClosure()
{
int a1 = 0;
var t = 0;
for (int i = 0; i < Count; i++)
{
t = 1;
Func<int, int> x = a => a + t;
a1 = x(a1);
};
return Count;
}
[Benchmark]
public int M_18_LambdaClosureLocal()
{
int a1 = 0;
for (int i = 0; i < Count; i++)
{
var t = 1;
Func<int, int> x = a => a + t;
a1 = x(a1);
};
return Count;
}
[Benchmark]
public int M_19_FuncCached()
{
int a = 0;
Func<int, int> x = Call;
for (int i = 0; i < Count; i++) a = x(a);
return Count;
}
[Benchmark]
public int M_20_FuncCachedInstance()
{
int a = 0;
Func<int, int> x = new CompareCallsBenchmark().CallInst;
for (int i = 0; i < Count; i++) a = x(a);
return Count;
}
[Benchmark]
public int M_21_FuncNew()
{
int a = 0;
for (int i = 0; i < Count; i++)
{
Func<int, int> x = Call;
a = x(a);
};
return Count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment