BenchmarkDotNet : Reliable and Efficient .NET Code Performance Measure

Atul Sharma

The performance of written code has always been a crucial and reliable measurement and getting them accurate is a big challenge. We always needed a tool that can provide us all the necessary matrices and then help us decide what is the best approach. In my initial experience with BenchmarkDotNet, I found it easiest to use among all free or paid code performance monitor tools.

What is this?

BenchmarkDotNet is a performance monitor tool that provides high-quality performance research. It helps to analyze the execution of a program and getting many useful statistics.

What is wrong with TimeSpan and Stopwatch?

Using TimeSpan :

I have seen many developers using TimeSpan to measure the time elapsed in program execution. But it is very immature to use TimeSpan as it doesn’t give accurate time.

See also  Let’s be Lazy with C# Lazy 😴😴

Using Stopwatch :

Stopwatch is more accurate than TimeSpan since it is very easy to use but it gives very limited information as elapsed time. In addition to this, it has few other problems as –

  1. Sometimes Stopwatch results are inconsistence.
  2. First execution takes longer time.
  3. It gives negative value as well as a weird result.

Despite these many issues with Stopwatch, I had used them in my many previous articles and analysis. In all of them, I had tried to run them in many iterations. Now onwards, I will be using this to compare the performance of different code snippets/features.

How to use BenchmarkDotNet?

Go to your solution and click on Manage Nuget Packages for Solution.

And you will see this screen.

  1. Search the Term “Benchmark”
  2. Click on the BenchmarkDotNet
  3. Select the project, where it will be used.
  4. Install this to the project(s)

This is applicable to the .NET Framework and .NET Core application.

Now, you will see the packages, which will be installed for your selected project.

After that, it will do the rest of the work and you will be ready to start a performance monitor for your application.

  • Now add [Benchmark] attribute to the method(s) you want to monitor or compare. Make sure to add namespace BenchmarkDotNet.Attributes;
  • Now use this code in main entry program BenchmarkRunner.Run<Test>(); and needed namespace for this is BenchmarkDotNet.Running;
    • Test is the name of the class where method(s) for benchmarking are available.
See also  Performance Consideration for C# Conditional Statements

Code Example

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BenchmarkSample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BenchmarkRunner.Run<Test>();
            Console.ReadLine();
        }
   }

    public class Test
    {
        [Benchmark]
        public void Method1()
        {
            Thread.Sleep(3000);
        }
        [Benchmark]
        public void Method2()
        {
            Thread.Sleep(2000);
        }
    }
}

Must have –

  1. All methods should be public.
  2. Methods or class should be instance not static.
  3. Code should be in release mode.

Output –

It generates a very detailed report about all the execution environment which you can see in the execution and this is the summary about the execution time –

Features

Other than execution time, Benchmark provides extensive detail of much other information as well, a few important features are below –

  1. We can get the performance of different .NET Environments.
  2. We can add additional information in the report.
  3. Very extensive statistics are available for code execution.
  4. We can baseline a method and then other methods with coming in relative execution.
  5. It allows us to log the report.

System and Environment Requirement:

  • IDE: Visual Studio 2015 or higher/ Rider
  • .NET Core 2.0 or higher
  • .NET Framework 4.6 or higher
  • NUGET 3.X+
  • OS: Windows, Linus, MacOS
  • Languages: C#, F#, VB

Read More –

In this next article, I have covered advanced features like memory allocation and generation-wise object count, and many more features with a real-life example.