Benchmarking in .NET – Explained

BenchmarkDotNet is a from the recent past, a very impressive valuable .NET library for benchmarking .NET code. It is designed to make it easy to write and run benchmarks for .NET code, and provides a range of features such as automatic warm-up, precision control, and support for multiple platforms.

To use BenchmarkDotNet, you first need to install the library using NuGet or another package manager. Once the library is installed, you can use it to write and run benchmarks for your .NET code.

Here is an example of how to use BenchmarkDotNet to benchmark a simple .NET method:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

public class BenchmarkExample
    public void MyMethod()
        // Code to be benchmarked goes here

    static void Main()
        var summary = BenchmarkRunner.Run<BenchmarkExample>();

To benchmark a method using BenchmarkDotNet, you simply need to decorate the method with the [Benchmark] attribute and then call BenchmarkRunner.Run to run the benchmark.

BenchmarkDotNet provides a range of options and features that can be used to customize and control the benchmarking process. For example, you can use the [Params] attribute to specify different input parameters for the benchmarked method, or use the [IterationTime] attribute to specify the target iteration time for the benchmark.

BenchmarkDotNet is a powerful and flexible library for benchmarking .NET code, and is widely used by developers and organizations looking to optimize the performance of their .NET code.

here is another old way of benchmarking which I used to use –

Stopwatch: The .NET Stopwatch class is a built-in .NET class that can be used to measure elapsed time. It provides a range of methods for starting and stopping the stopwatch, and for measuring elapsed time in various units.

    Using the .NET Stopwatch class to benchmark a simple .NET method:

    using System.Diagnostics;
    public static void Main()
        Stopwatch stopwatch = new Stopwatch();
        // Code to be benchmarked goes here
        Console.WriteLine("Elapsed time: {0}", stopwatch.Elapsed);

    These are just a few examples of the tools and libraries available for benchmarking .NET code. There are many other options available, and the best choice will depend on the specific needs and requirements of your project.

    Leave a Reply

    Please log in using one of these methods to post your comment: Logo

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

    Facebook photo

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

    Connecting to %s

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Create a website or blog at

    Up ↑