Skip to content

Instantly share code, notes, and snippets.

@xoofx
Created February 22, 2018 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoofx/1cad75ae6783ffc2bc18f88c1612a68d to your computer and use it in GitHub Desktop.
Save xoofx/1cad75ae6783ffc2bc18f88c1612a68d to your computer and use it in GitHub Desktop.
Bench Virtual Call vs Delegate Call
// BenchmarkDotNet=v0.10.12, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.248)
// Intel Core i7-4980HQ CPU 2.80GHz (Haswell), 1 CPU, 8 logical cores and 4 physical cores
// Frequency=2728070 Hz, Resolution=366.5595 ns, Timer=TSC
// [Host] : .NET Framework 4.6.1 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.2633.0
// DefaultJob : .NET Framework 4.6.1 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.2633.0
//
//
// Method | Mean | Error | StdDev |
// ------------- |----------:|----------:|----------:|
// VirtualCall | 0.9152 ns | 0.0393 ns | 0.0368 ns |
// DelegateCall | 1.1955 ns | 0.0343 ns | 0.0321 ns |
using BenchmarkDotNet.Attributes;
namespace TestBenchmark
{
public class BenchVirtualVsDelegate
{
private readonly ProcessorBase _processor;
private readonly ProcessDelegate _processorDelegate;
public delegate int ProcessDelegate(int a);
public BenchVirtualVsDelegate()
{
_processor = new MyProcessor();
_processorDelegate = i => i + 1;
}
[Benchmark]
public int VirtualCall()
{
return _processor.Process(0);
}
[Benchmark]
public int DelegateCall()
{
return _processorDelegate(0);
}
private abstract class ProcessorBase
{
public abstract int Process(int a);
}
private class MyProcessor : ProcessorBase
{
public override int Process(int a)
{
return a + 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment