如何对需要参数的不同类别的方法进行基准测试?

huangapple go评论96阅读模式
英文:

How to benchmark methods from different classes that require arguments?

问题

  1. 我想使用 [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) 来对以下三个类的 `Run()` 方法运行基准测试。但是不清楚语法应该怎样写。
  2. ``` cs
  3. class Class1
  4. {
  5. [Benchmark]
  6. public bool Run(ref string[] columns)
  7. {
  8. // ...
  9. }
  10. }
  11. class Class2
  12. {
  13. [Benchmark]
  14. public bool Run(ref string[] columns)
  15. {
  16. // ...
  17. }
  18. }
  19. class Class3
  20. {
  21. [Benchmark]
  22. public bool Run(ref string[] columns)
  23. {
  24. // ...
  25. }
  26. }

我尝试了如下的语法。

  1. BenchmarkRunner.Run<Class1>();

但是这给了我一个错误。

> Benchmark 方法 ReadRow 具有不正确的签名。
> 方法不应该有任何参数。

问题:
为了比较这三种方法的性能:

  • 我怎样满足参数要求并消除这个错误?
  • 我怎样比较这些方法的性能?我只需要依次对每个类调用 BenchmarkRunner.Run<Class1>() 吗?
  1. <details>
  2. <summary>英文:</summary>
  3. I want to use [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet) to run benchmarks on all three `Run()` methods of the following classes. But it&#39;s not clear what the syntax should be.
  4. ``` cs
  5. class Class1
  6. {
  7. [Benchmark]
  8. public bool Run(ref string[] columns)
  9. {
  10. // ...
  11. }
  12. }
  13. class Class2
  14. {
  15. [Benchmark]
  16. public bool Run(ref string[] columns)
  17. {
  18. // ...
  19. }
  20. }
  21. class Class3
  22. {
  23. [Benchmark]
  24. public bool Run(ref string[] columns)
  25. {
  26. // ...
  27. }
  28. }

I tried syntax like this.

  1. BenchmarkRunner.Run&lt;Class1&gt;();

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&lt;Class1&gt;() for each class, sequentially?

答案1

得分: 2

  1. &gt; 如何满足参数要求并消除此错误?
  2. 创建包含无参数基准方法的类,并在其中调用基准方法,传递参数。可以在现场创建参数,也可以使用预先创建的参数:
  3. ```csharp
  4. class MyBenchmark
  5. {
  6. [Benchmark]
  7. public bool RunClass1()
  8. {
  9. var columns = // 创建 string[]; 可能使用字段来存储它
  10. new Class1().BenchmarkedMethod(ref columns);
  11. }
  12. }

还要查看有关的文档文章:

如果需要,指定的方法将允许将实例和参数创建移出基准方法。

> 如何比较方法的性能

将所有方法移动到一个基准类中。

  1. class MyBenchmark
  2. {
  3. [Benchmark]
  4. public bool RunClass1()
  5. {
  6. var columns = // 创建 string[];
  7. return new Class1().BenchmarkedMethod(ref columns);
  8. }
  9. [Benchmark]
  10. public bool RunClass2()
  11. {
  12. var columns = // 创建 string[];
  13. return new Class2().BenchmarkedMethod(ref columns);
  14. }
  15. }
  1. <details>
  2. <summary>英文:</summary>
  3. &gt; How do I satisfy the argument requirements and eliminate this error?
  4. 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:
  5. ```csharp
  6. class MyBenchmark
  7. {
  8. [Benchmark]
  9. public bool RunClass1()
  10. {
  11. var columns = // create string[]; maybe use field to store it
  12. new Class1().BenchmarkedMethod(ref columns);
  13. }
  14. }

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.

  1. class MyBenchmark
  2. {
  3. [Benchmark]
  4. public bool RunClass1()
  5. {
  6. var columns = // create string[];
  7. return new Class1().BenchmarkedMethod(ref columns);
  8. }
  9. [Benchmark]
  10. public bool RunClass2()
  11. {
  12. var columns = // create string[];
  13. return new Class2().BenchmarkedMethod(ref columns);
  14. }
  15. }

huangapple
  • 本文由 发表于 2023年2月19日 23:25:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定