Skip to content

Instantly share code, notes, and snippets.

@badamczewski
Last active August 4, 2021 20:56
Show Gist options
  • Save badamczewski/2dde1daf21f430851a4bdf3a616409fd to your computer and use it in GitHub Desktop.
Save badamczewski/2dde1daf21f430851a4bdf3a616409fd to your computer and use it in GitHub Desktop.
Bench
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApp1
{
public class Program
{
static void Main(string[] args) {
var summary = BenchmarkRunner.Run<Bench95>();
}
}
[DisassemblyDiagnoser]
public class Bench95
{
private int[] _a = new int[1000];
private int _i = 1000;
[GlobalSetup]
public void Setup()
{
_a[0] = 1;
}
[Benchmark(Baseline = true)]
[MethodImpl(MethodImplOptions.NoInlining)]
public void A()
{
var i = _i;
var a = _a;
LoopA(a, i);
}
[MethodImpl(MethodImplOptions.NoInlining)]
void LoopA(int[] a, int i)
{
do
{
if (a[0] == 0)
break;
i--;
}
while (i != 0);
}
[Benchmark()]
[MethodImpl(MethodImplOptions.NoInlining)]
public void B()
{
var i = _i;
var a = _a;
LoopB(a, i);
}
[MethodImpl(MethodImplOptions.NoInlining)]
void LoopB(int[] a, int i)
{
while (a[0] != 0)
{
i--;
if (i == 0)
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment