Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to benchmark methods from different classes that require arguments?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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 for option for:

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);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading