Getting started with BenchmarkDotNet


Published on

January 19, 2023

In a very short time, BenchmarkDotNet has become a standard for performance benchmarking in the .NET world. It’s used by Microsoft for his benchmarking analyses and by .NET developers, in general. In a few lines of codes, you can have a good UI in a console application but it produces good charts as well.

Let’s see how is easy to getting started with BenchmarkDotNet in a new simple project.

Create a new Console Application with the command:

dotnet new console

Install the nuget package from commandline:

dotnet add package BenchmarkDotNet --version 0.13.4

Now you can open Visual Studio Code with the following command:

code .

In the Program.cs file, remove the existing code and paste the code below:

using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;


public class StringsBenchmark
    public int Iterations;
    private const string TestString = "This is a test string";

    public void StringConcat()
        string result = string.Empty;
        for (int i = 0; i < Iterations; i++)
            result += TestString;

    public void StringBuilder()
        var sb = new StringBuilder();
        for (int i = 0; i < Iterations; i++)

For this sample I have created a StringsBenchmark class with two methods inside. These two methods are the methods I want to measure. It’s easy to recognize them because they are marked with the attribute Benchmark.

The variable called “Iterations” is normal variable but with the attribute “Params” and the list of the values, it becomes a special variable for benchmarking. In this case the methods inside the class will be called three times with these different values.

To launch the benchmark, all you have to do is call the method Run of the BenchmarkRunner with the name of the benchmark class. Of course, you can call more than one benchmark in the same console application.

Come back to the command line and type the following command:

dotnet run -c release

The benchmark works only in Release mode because it doesn’t make sense to obtain results from a not optimized executable file.

After a while, you will obtain the following result in the the console.

Image description

In addition, if you navigate to the folder “BenchmarkDotNet.Artifacts/results” you will find three files.

Image description

These are the default reports for Benchmarkdotnet but I will write you another post about the reports in the next weeks.


As you can see it’s very easy to getting started with benchmarkdotnet but at the same time in this article we scratched only the top of the iceberg. In the next articles we will go more deep with real cases and with advanced reports.

Are you interested in learning GitHub but don't know where to start? Try my course on LinkedIn Learning: Learning GitHub.

LinkedIn Learning

Thanks for reading this post, I hope you found it interesting!

Feel free to follow me to get notified when new articles are out 🙂

{% embed https://dev.to/kasuken %}

React, comment and follow on