Benchmark Your .NET Code with BenchmarkDotNet

Dilanka Muthukumarana
3 min readJul 25, 2024

--

Hello everyone! 🌟 Today, we’re diving into how to benchmark your .NET or C# code using the BenchmarkDotNet package.

What is Benchmarking? 📊

Benchmarking involves running a computer program to assess the performance of various methods. If you have multiple methods and want to know how well they perform, benchmarking is your go-to tool.

Why BenchmarkDotNet? 🔍

BenchmarkDotNet is a community-created package widely used across various projects. It allows us to write benchmarks easily and efficiently.

Setup Your Benchmark

First, you need to install the BenchmarkDotNet NuGet package in your project.
https://www.nuget.org/packages/BenchmarkDotNet

dotnet add package BenchmarkDotNet --version 0.13.12

Here’s a quick rundown.

Define and Create the Program

Create a class that contains the methods we want to benchmark.

using System;
using System.Diagnostics;
using System.Drawing;
namespace EnumBenchmark;

enum Colors { Red, Green, Blue }

public class EnumBenchmarker
{

public void BenchmarkToString(int iterations)
{

for (int i = 0; i < iterations; i++)
{
var name = Colors.Red.ToString();
}
}

public void BenchmarkEnumGetName(int iterations)
{

for (int i = 0; i < iterations; i++)
{
var name = Enum.GetName(typeof(Colors), Colors.Red);
}

}

public void BenchmarkNameOf(int iterations)
{

for (int i = 0; i < iterations; i++)
{
var name = nameof(Colors.Red);
}

}
}

Create the Benchmark Executor

This step is similar to running unit tests in our business logic classes. We set up a class to execute the benchmarks and use the [Benchmark] attribute to mark the methods for benchmarking.

using BenchmarkDotNet.Attributes;
using EnumBenchmark;

namespace ConsoleAppTry;

public class EnumBenchmarkExecutor
{
const int iterations = 100000000;
[Benchmark]
public void BenchmarkEnumGetName(){
EnumBenchmarker enumBenchmarker= new EnumBenchmarker();
enumBenchmarker.BenchmarkEnumGetName(iterations);
}

[Benchmark]
public void BenchmarkNameOf(){
EnumBenchmarker enumBenchmarker= new EnumBenchmarker();
enumBenchmarker.BenchmarkNameOf(iterations);
}

[Benchmark]
public void BenchmarkToString(){
EnumBenchmarker enumBenchmarker= new EnumBenchmarker();
enumBenchmarker.BenchmarkToString(iterations);
}
}

Run the Benchmark

Finally, set up a program.cs to run the benchmarks. This is analogous to running unit tests where we execute our test methods and observe the results.

using BenchmarkDotNet.Running;
using ConsoleAppTry;
using EnumBenchmark;


BenchmarkRunner.Run<EnumBenchmarkExecutor> ();

Running the Benchmark 🏃‍♂️

Once you have set up your classes, run the benchmark to get detailed results about the performance of each method. BenchmarkDotNet will provide insights into execution time and memory allocation.

dotnet run -c Release

Results and Analysis 📈

The results will show you which method is the fastest and usage of memory. Here’s a summary of what each method does:

  • ToString(): Converts the enum value to its string representation.
  • Enum.GetName(): Gets the name of the enum value.
  • nameof(): Gets the name of the enum value at compile time.

Conclusion 🏁

Benchmarking different methods for enum name retrieval helps you understand the trade-offs and make informed decisions about which method to use in performance-critical applications. This introduction demonstrates how easy it is to start with BenchmarkDotNet, but it’s just a small feature of what you can achieve with this powerful tool.

BenchmarkDotNet offers a set of features to explore.

  • Advanced Reporting — Generate detailed reports in various formats (HTML, CSV, Markdown) to share your results with others.
  • Memory Diagnostics — Analyze memory allocations and garbage collection impacts on your benchmarks.
  • Parameterization — Run benchmarks with different input parameters to see how performance varies with different data sets.
  • Hardware Counters — Utilize CPU hardware counters for low-level performance insights.
  • Environment Analysis — Automatically include information about the runtime environment in your benchmark results.

For more information about these features and more, check out the BenchmarkDotNet GitHub project

If you enjoyed this article and found it insightful, please consider supporting it with some 👏 claps, sharing it 🔄, and following me on LinkedIn 🔗. I value your feedback and would love to hear your opinions and ideas 💡. Don’t hesitate to comment below with topics you’re interested in or thoughts you’d like to share 💬. Let’s keep the conversation going and explore together!

--

--