0

I want to use BenchmarkDotNet to run benchmarks on all three Run() methods of the following classes. But it's not clear what the syntax should be.

class Class1
{
    [Benchmark]
    public bool Run(ref string[] columns)
    {
        // ...
    }
}

class Class2
{
    [Benchmark]
    public bool Run(ref string[] columns)
    {
        // ...
    }
}

class Class3
{
    [Benchmark]
    public bool Run(ref string[] columns)
    {
        // ...
    }
}

I tried syntax like this.

BenchmarkRunner.Run<Class1>();

But this gives me an error.

Benchmark method ReadRow has incorrect signature.
Method shouldn't have any arguments.

Questions: To compare the performance of these three methods:

  • How do I satisfy the argument requirements and eliminate this error?
  • How do I compare the performance of the methods? Do I simply call BenchmarkRunner.Run<Class1>() for each class, sequentially?
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

1 Answers1

2

How do I satisfy the argument requirements and eliminate this error?

Create class containing parameterless benchmark method and invoke the becnhmarked method there passing parameters. Either create the param in-place or use some precreated ones:

class MyBenchmark
{
    [Benchmark]
    public bool RunClass1()
    {    
        var columns = // create string[]; maybe use field to store it
        new Class1().BenchmarkedMethod(ref columns);
    }
}

Also check the docs articles on:

If needed approaches specified there will allow to move instance and parameter creation out of the benchmarked methods.

How do I compare the performance of the methods

Move all method inside one benchmark class.

class MyBenchmark
{
    [Benchmark]
    public bool RunClass1()
    {    
        var columns = // create string[];
        return new Class1().BenchmarkedMethod(ref columns);
    }

    [Benchmark]
    public bool RunClass2()
    {    
        var columns = // create string[];
        return new Class2().BenchmarkedMethod(ref columns);
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    Thanks, but now I'm allocating class instances and that time is being included in the benchmark. – Jonathan Wood Feb 19 '23 at 15:45
  • @JonathanWood without seeing the actual code it is hard to tell the best approach, but as written in the comment in the first snippet - you can move the instance creation to field. Also check the links for samples. – Guru Stron Feb 19 '23 at 15:48