Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created February 25, 2018 14:01
Show Gist options
  • Save EgorBo/7c39d9ecde205cecfdeb407f67271d77 to your computer and use it in GitHub Desktop.
Save EgorBo/7c39d9ecde205cecfdeb407f67271d77 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace MyBenchmarks
{
public class Tests
{
private const int N = 10000;
private readonly byte[] data;
[Benchmark]
public void Class1()
{
var pp = new Class1();
pp.Foo();
}
[Benchmark]
public void Class2()
{
var pp = new Class2();
pp.Foo();
}
[Benchmark]
public void Class2_without_callvirt()
{
new Class2().Foo();
}
[Benchmark]
public void Class3()
{
IClass3 pp = new Class3();
pp.Foo();
}
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Tests>();
}
}
public class Class1
{
int a = 10;
[MethodImpl(MethodImplOptions.NoInlining)]
public void Foo()
{
a++;
}
}
public class Class2
{
int a = 10;
public void Foo()
{
a++;
}
}
public interface IClass3
{
void Foo();
}
public class Class3 : IClass3
{
int a = 10;
public void Foo()
{
a++;
}
}
}
/*
BenchmarkDotNet=v0.10.12, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.248)
Intel Core i7-8700K CPU 3.70GHz (Coffee Lake), 1 CPU, 12 logical cores and 6 physical cores
Frequency=3609373 Hz, Resolution=277.0564 ns, Timer=TSC
[Host] : .NET Framework 4.7 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.2633.0
DefaultJob : .NET Framework 4.7 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.2633.0
Method | Mean | Error | StdDev |
------------------------ |---------:|----------:|----------:|
Class1 | 2.266 ns | 0.0230 ns | 0.0215 ns |
Class2 | 1.416 ns | 0.0452 ns | 0.0423 ns |
Class2_without_callvirt | 1.375 ns | 0.0090 ns | 0.0080 ns |
Class3 | 2.534 ns | 0.0075 ns | 0.0066 ns |
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment