We’ve talked a lot on this blog about getting started with various technologies and different ways to architect your code or your solution. We’ve also talked about testing and measuring performance. But one thing we do a lot as software engineers is refactor. As a professional engineer, it is your responsibility to make your product’s code better each time you touch it. Without continuous improvement in our code, it will begin to rot. You can get a sense if the code is easier to read by, well, reading it. But how do you know if the code performs better? That’s where benchmarking comes in.
Benchmarking is the act of running a computer program multiple times in order to assess its relative performance. Common metrics include execution time and memory usage. One big challenge in benchmarking is that things take longer when the system is cold than when it’s warm. So how do we benchmark and how do we account for the variations in our systems? There’s a handy NuGet package that can take all the guesswork out of it called BenchmarkDotNet.
Getting Your Hands on Benchmarks
If you’ve worked with .NET for any length of time, you know how expensive it is to work with strings. The string class is immutable; each time you manipulate a string, it gets recreated and the previous memory is cleaned up. This is why Microsoft has added StringBuilder, string interpolation, and other methods of working with strings. For the purposes of our benchmark, we’re going to compare basic string manipulation against StringBuilder.
To get started, create a new Console Application in Visual Studio or VS Code. Next, install the BenchmarkDotNet NuGet package and you’re ready to start benchmarking.
The first thing we’re going to do is create a class called MyTestClass which will hold the methods that we want to test. Next, we’ll create a method to do classic string manipulation. (I would NEVER write code like this in real life, so it feels wrong to do this.)
Take note of the Benchmark attribute added to our method. This tells BenchmarkDotNet that this is a method we want to measure.
The next thing we’re going to do is create a second method to do the same logic using a StringBuilder.
We can see that StringBuilder performs significantly faster than string manipulation. You could do the math to figure out how much faster, but BenchmarkDotNet can do that for you! All you have to do is tag your primary method with Baseline = true like so:
Using StringBuilder takes 17% of the time that basic string manipulation takes on my machine.
Sometimes you want to see how much more or less memory one approach takes vs another. BenchmarkDotNet handles that too with its MemoryDiagnoser attribute. Tagging the class that contains your methods with this attribute will export information about the memory usage of your algorithm:
| Method | Mean | Gen 0 | Allocated |
| GiveMeAnA | 1,918.6 ns | 3.0060 | 12,576 B |
| GiveMeAnAWithStringBuilder | 314.6 ns | 0.1836 | 768 B |
Simple string addition uses 12,575 bytes of memory and incurs 3 garbage collections on Generation 0 per 1,000 operations while StringBuilder only needs 768 bytes and performs less than 1 garbage collection per 1,000 operations. Garbage collection is expensive, so not only does is StringBuilder significantly faster and use a lot less memory, it also performs much fewer expensive garbage collection calls.
For reference, here is the full source code of our final solution:
By benchmarking your project, you can get real numbers to help you show how much your changes improved your code. With things like the memory allocation, you can quantify how much less memory an proposed solution will use if you’re trying to solve an issue with memory pressure in production.
BenchmarkDotNet includes many more metrics and allows you to compare execution stats of your algorithm against different version of .NET Core, .NET Standard, and .NET Framework. We’ve barely scratched the surface, but hopefully you’ve been inspired to dig deeper and see what else it can teach you about your code.
In Part II of this series, we’ll look at how you can evaluate a larger refactor under production load. Until then, happy benchmarking!