我有一个非常标准的基准测试方法,它计算方法可以在 60 秒内运行的次数:
public static int BenchmarkTasks(Action<int> func, List<int> ids, int seconds)
{
var watch = new Stopwatch();
var executionCount = 0;
watch.Start();
foreach (var id in ids)
{
if (watch.ElapsedMilliseconds / 1000 > seconds)
return executionCount;
func(id);
executionCount++;
}
watch.Stop();
return executionCount;
}
但是,我正在尝试弄清楚如何正确调用此方法 - 例如:
var ids = new List<int> {1, 2, 3};
BenchmarkHelper.BenchmarkTasks(delegate { MethodToBenchmark(-1); }, ids, 60);
只是运行 MethodToBenchmark 方法,将 -1 作为输入值(可以理解),它不会遍历我的 ID 集合。
谁能告诉我哪里出错了?当然,这只是我误解了如何执行此操作。
谢谢。
请您参考如下方法:
您不需要委托(delegate) - 您只需要直接传递该方法。否则,您将传递一个每次调用 MethodToBenchmark(-1) 的 Action。
你应该这样调用它:
var ids = new List<int> {1, 2, 3};
BenchmarkHelper.BenchmarkTasks(MethodToBenchmark, ids, 60);
