英文:
How to benchmark methods from different classes that require arguments?
问题
我想使用 [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) 来对以下三个类的 `Run()` 方法运行基准测试。但是不清楚语法应该怎样写。
``` cs
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)
{
// ...
}
}
我尝试了如下的语法。
BenchmarkRunner.Run<Class1>();
但是这给了我一个错误。
> Benchmark 方法 ReadRow 具有不正确的签名。
> 方法不应该有任何参数。
问题:
为了比较这三种方法的性能:
- 我怎样满足参数要求并消除这个错误?
- 我怎样比较这些方法的性能?我只需要依次对每个类调用
BenchmarkRunner.Run<Class1>()
吗?
<details>
<summary>英文:</summary>
I want to use [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) to run benchmarks on all three `Run()` methods of the following classes. But it's not clear what the syntax should be.
``` cs
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?
答案1
得分: 2
> 如何满足参数要求并消除此错误?
创建包含无参数基准方法的类,并在其中调用基准方法,传递参数。可以在现场创建参数,也可以使用预先创建的参数:
```csharp
class MyBenchmark
{
[Benchmark]
public bool RunClass1()
{
var columns = // 创建 string[]; 可能使用字段来存储它
new Class1().BenchmarkedMethod(ref columns);
}
}
还要查看有关的文档文章:
如果需要,指定的方法将允许将实例和参数创建移出基准方法。
> 如何比较方法的性能
将所有方法移动到一个基准类中。
class MyBenchmark
{
[Benchmark]
public bool RunClass1()
{
var columns = // 创建 string[];
return new Class1().BenchmarkedMethod(ref columns);
}
[Benchmark]
public bool RunClass2()
{
var columns = // 创建 string[];
return new Class2().BenchmarkedMethod(ref columns);
}
}
<details>
<summary>英文:</summary>
> 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:
```csharp
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论