Various .NET Benchmarks

.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:

The following code is needed in the Program Main

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

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");
    }
}

Pretty simple isn’t it?

After the run the result is the following:

MethodMeanErrorStdDevScaledScaledSDGen 0Gen 1Allocated
'DateTime ToString'848.8 ns2.8059 ns2.4874 ns1.000.000.0410-132 B
'DateTime ToString with format'790.0 ns18.0956 ns16.0413 ns0.930.020.03910.0104124 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:

Thanks and enjoy!

comments powered by Disqus