Benchmarking with BenchmarkDotNet
In this section, we will benchmark some methods to determine which method gives us the best performance. Keep in mind that there is some initial expense when running code in parallel. So, sometimes, parallel code may not be the best option for improving code performance. Let’s get started:
- Comment out the code in the
Main
method and add the following line:BenchmarkRunner.Run<Benchmarks>();
- Add a class called
Benchmarks
. - Add the following
NuGet
packages:BenchmarkDotNet
LinqOptimizer.Csharp
- Add the
using
statements for each of theNuGet
packages to theBenchmarks
class. - Add the following code to set up our benchmarks:
private short[] data; [GlobalSetup] public void GlobalSetup() { integers = new Int16[Int16.MaxValue]; for (short x = 1; x <= integers.Length - 1; x++) { integers[x] = x; } ...