BenchmarkDotNet
Context: BenchmarkDotNet is a powerful open‑source library that turns your methods into benchmarks, runs them multiple times, and provides statistical results (mean, error, std dev, allocation). It automatically handles warm‑up, iterations, and memory diagnostics. To use it, add the BenchmarkDotNet NuGet package and mark methods with [Benchmark].
Usage Example
Section titled “Usage Example”dotnet add package BenchmarkDotNetusing BenchmarkDotNet.Attributes;using BenchmarkDotNet.Running;using System;
public class MyBenchmarks{ [Benchmark] public int CalculateSum() { int sum = 0; for (int i = 0; i < 1000; i++) sum += i; return sum; }}
class Program{ static void Main(string[] args) { var summary = BenchmarkRunner.Run<MyBenchmarks>(); }}Output console
Section titled “Output console”dotnet run -c Release| Method | Mean | Error | StdDev ||-------------- |---------:|---------:|---------:|| CalculateSum | 0.524 us | 0.012 us | 0.011 us |Important notes
Section titled “Important notes”- Build and run in Release mode:
dotnet run -c Release - Do not attach a debugger (Ctrl+F5 in VS).
- BenchmarkDotNet will execute each benchmark many times for reliable statistics.
Real‑world usage example
Section titled “Real‑world usage example”Comparing serializers – Benchmark System.Text.Json vs Newtonsoft.Json to see which is faster for your data.
See BenchmarkDotNet getting started.