Sample: IntroArguments
As an alternative to using [Params]
,
you can specify arguments for your benchmarks.
There are several ways to do it (described below).
The [Arguments]
allows you to provide a set of values.
Every value must be a compile-time constant (it's C# language limitation for attributes in general).
You can also combine
[Arguments]
with
[Params]
.
As a result, you will get results for each combination of params values.
Source code
using System.Threading;
using BenchmarkDotNet.Attributes;
namespace BenchmarkDotNet.Samples
{
public class IntroArguments
{
[Params(true, false)] // Arguments can be combined with Params
public bool AddExtra5Milliseconds;
[Benchmark]
[Arguments(100, 10)]
[Arguments(100, 20)]
[Arguments(200, 10)]
[Arguments(200, 20)]
public void Benchmark(int a, int b)
{
if (AddExtra5Milliseconds)
Thread.Sleep(a + b + 5);
else
Thread.Sleep(a + b);
}
}
}
Output
| Method | AddExtra5Miliseconds | a | b | Mean | Error | StdDev |
|---------- |--------------------- |---- |--- |---------:|----------:|----------:|
| Benchmark | False | 100 | 10 | 110.1 ms | 0.0056 ms | 0.0044 ms |
| Benchmark | False | 100 | 20 | 120.1 ms | 0.0155 ms | 0.0138 ms |
| Benchmark | False | 200 | 10 | 210.2 ms | 0.0187 ms | 0.0175 ms |
| Benchmark | False | 200 | 20 | 220.3 ms | 0.1055 ms | 0.0986 ms |
| Benchmark | True | 100 | 10 | 115.3 ms | 0.1375 ms | 0.1286 ms |
| Benchmark | True | 100 | 20 | 125.3 ms | 0.1212 ms | 0.1134 ms |
| Benchmark | True | 200 | 10 | 215.4 ms | 0.0779 ms | 0.0691 ms |
| Benchmark | True | 200 | 20 | 225.4 ms | 0.0775 ms | 0.0725 ms |
Links
- Parameterization
- The permanent link to this sample: BenchmarkDotNet.Samples.IntroArguments