0

我想将以下代码提取到一个新方法中:

...
Parallel.For(0, Environment.ProcessorCount, i => 
       { handTypeSum[i] = new PSTLooper(intHands).EvalEnumeration(i); });
...

PSTLooper是类型IEvaluator,我还有其他几个IEvaluator我想用这种方法测试。该方法应该尽可能快地执行,现在我对 Parallel.For 的性能非常满意(我很想了解更快/更好的方法)。

我需要为每个线程生成一个新对象,并为我的EvalEnumeration(int instance)方法生成当前线程数。由于这些限制,几次尝试都失败了。

我的一些尝试:


StartNewTest(new PSTLooper(intHands));

public void StartNewTest(IEvaluator)
{
     Parallel.For(0, Environment.ProcessorCount, i => 
          { handTypeSum[i] = e.EvalEnumeration(i); });
}

该方法可以编译,但仅使用IEvaluator并且不创建新方法。


StartNewTest(new PSTLooper(intHands).EvalEnumeration());

public void StartNewTest(Func<long[]> func)
{
     Parallel.For(0, Environment.ProcessorCount, i => 
          { handTypeSum[i] = func.Invoke(); });
}

那不编译,因为我需要# of Instance。


我很确定我的方法不是最好的,但现在我不知道更好,因此需要在这里问这个问题。

4

1 回答 1

1

这对你有用吗?

StartNewTest(i => new PSTLooper(intHands).EvalEnumeration(i));

public void StartNewTest(Func<int, long[]> func)
{
     Parallel.For(0, Environment.ProcessorCount, i => 
          { handTypeSum[i] = func(i); });
}
于 2011-09-15T11:21:43.663 回答