DEV Community

Sotirios Mantziaris
Sotirios Mantziaris

Posted on • Edited on

Various .NET Benchmarks

A lot of times i was wondering what is the best performing code.
In order to determine that i had to benchmark my code, but benchmarks are hard to write.
Luckily there is a open source project that does this work perfectly good and very easy.

The name of the library is BenchmarkDotNet and the documentation can be found here.
The only thing you have to do is:

  • Create a console application project (.NET/.NET Core)
  • Install the nuget BenchmarkDotNet and it's dependencies
  • Always run in release mode!

The following code is needed in the Program Main

public static void Main(string[] args)
{
    BenchmarkSwitcher.FromAssembly(typeof(Program)
                    .GetTypeInfo().Assembly).Run(args);
}
Enter fullscreen mode Exit fullscreen mode

which will scan the assembly for benchmarks and asks you which one to run.

A typical benchmark is just a class with methods that are annotated with the [Benchmark] attribute like the following one:

[MemoryDiagnoser]
public class DateTimeToStringBenchmark
{
    private static readonly DateTime _dateTime = 
        new DateTime(2016, 12, 31, 23, 59, 59, 999);

    [Benchmark(Baseline = true, Description = "DateTime ToString")]
    public string DateTimeToString()
    {
        return _dateTime.ToString();
    }

    [Benchmark(Description = "DateTime ToString with format")]
    public string DateTimeToStringFormat()
    {
        return _dateTime.ToString("g");
    }
}
Enter fullscreen mode Exit fullscreen mode

Pretty simple isn't it?

After the run the result is the following:

Method Mean Error StdDev Scaled ScaledSD Gen 0 Gen 1 Allocated
'DateTime ToString' 848.8 ns 2.8059 ns 2.4874 ns 1.00 0.00 0.0410 - 132 B
'DateTime ToString with format' 790.0 ns 18.0956 ns 16.0413 ns 0.93 0.02 0.0391 0.0104 124 B

I get the timings of the method and by using the [MemoryDiagnoser] attribute i get event the GC Stats.

Conducted Benchmarks

Given the easiness to create such benchmark i have started a github repository named DotNetBenchmarks.

There you can find some of the performance questions that i have about some components like logging frameworks, XML Serialization, String concatenation etc.
Every time i have to check the performance of some component i will add a new benchmark to this repository. If anyone likes to contribute, even better!
Make a PR!

Please note that:

  • These benchmark are not conducted in order to make certain libraries look good or bad
  • Please create a issue or better make a PR if you think that the benchmark methodology is wrong or the setup is wrong
  • Do not take the results as granted and conduct your own benchmarks to see if in the context of your application the results differ

Thanks and enjoy!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video 📹️

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay