Introduction to BenchMarkDotNet

There are many powerful libraries/packages available in the NuGet Package Manager. One of the most powerful library is BenchMarkDotNet. It helps you to transform methods into benchmarks, track their performance, and also share reproducible measurable experiments. The respsective library is being used in 3000+ projects.

The results will be shown in an user friendly fashion where you can get an clear vision of it. The main four aspects which defines the design of these features are,

  • Simplicity
  • Automation
  • Reliability
  • Friendliness

This library is available for both .NET Framework and .NET Core.

In this blog, we are going to benchmark two simple methods which is mostly used in C#,

  • FirstOrDefault
  • SingleOrDefault

Benchmark FirstOrDefault and SingleOrDefault

SingleOrDefault

The main difference between these two methods is, SingleOrDefault clearly state that the query should result in utmost a single result.

FirstOrDefault

On the other hand, FirstOrDefault will return any amount of results where it will pick the first occurrence.

Step 1:

Open Visual Studio 2019 and select Console App(.NET Framework) in the listed project templates and Click Next.

Step 2:

Mention the project name and the directory of your solution and click Create.

Step 3:

A console app will be created and first we are going the install the Benchmark .NET package for your project. So Right-click on your project and select Manage NuGet Packages.

And search for BenchMark and you will receive a suggestion called BechmarkDotNet. Select the respective package and click install.

Step 4:

All Set. So we are going to create a class called BechMarkDotNet which will have two methods GetNameByFirstOrDefault() and GetNameBySingleOrDefault().

public class BenchMarkDotNet
{
   public string GetNameByFirstOrDefault()
   {
      .   .  .  .  .  .  .
   }

		
   public string GetNameBySingleOrDefault()
   {
      .   .   .   .   .    .   
   }
}

Step 5:

Next up, we are going to define a string array and we are going to get the first value which matches nullablereference.

[BenchMark]
public string GetNameByFirstOrDefault()
{
   var names = new List<string>
   {
      "nullablereference",
      "efficientuser"
   };
   return names
         .FirstOrDefault(x => x.Equals("nullablereference"));
}
[BenchMark]
public string GetNameBySingleOrDefault()
{
  var names = new List<string>
  {
    "nullablereference",
    "efficientuser"
  };
  return names
        .SingleOrDefault(x => x.Equals("nullablereference"));
}

And also on the above code snippets we have introduced [BechMark] attribute for both these functions to benchmark the performance respectively.

Step 6:

The final step is that we have to run these functions to benchmark their performance. To do that you want to use BenchMarkRunner.Run().

internal class Program
{
   private static void Main(string[] args)
   {
      BenchmarkRunner.Run<BenchMarkDotNet>();
      Console.ReadLine();
   }
}

Step 7:

Build the project and run it by pressing F5 key. And also make sure you run your project in Release mode. Once you pressed, it will perform some benchmarks by making various requests.

And once it is done, it will show you the the result in an table format like below,

We have benchmarked two mostly used methods in C# and by the provided result you can see FirstOrDefault is perfoming well when compared to SingleOrDefault.

Conclusion

This library will be very useful to benchmark the performance and also it provides us an accurate result. In this blog, we used it to benchmark two methods but you can also use it to benchmark two NPM libraries. And as I said earlier, this library is also available for .NET Core.

BenchMarkDotNet Demo Link => https://github.com/SuhasParameshwara/BenchMarkDotNet_Demo

In our next blog, we will explore an another famous .NET library called AutoMapper.

Happy Coding!

Cheers! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: