Benchmark DotNet
Method-level Benchmarking Using BenchmarkDotNet
Here is an example of method-level benchmarking using BenchmarkDotNet.
The BenchmarkDotNet site provides detailed instructions for setup.
The following example shows a basic setup to benchmark two C# methods.
Creating a Console Application for Benchmarking
First, create a console application and install the BenchmarkDotNet Nuget package:
PM> Install-Package BenchmarkDotNet
The following example benchmarks two different methods of assigning new values based on comparisons:
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace BenchmarkZoo
{
public class MyBenchmark
{
private bool _b1, _b2;
// Add the [Benchmark] attribute for methods you want to benchmark.
[Benchmark]
public void RunAssignmentWithOr()
{
_b1 = _b1 | _b2;
}
[Benchmark]
public void RunAssignmentWithReturn()
{
if (_b1) return;
_b1 = _b2;
}
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<MyBenchmark>();
Console.WriteLine(summary);
Console.ReadKey();
}
}
}
Running the console program will run your annotated methods several times and generate a report including min, max, median, mean, and standard deviation for run times.