开发者

Problems with extracting a method because of parallelism

I would like to extract following code into a new method:

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

PSTLooper is of type IEvaluator and I have several other IEvaluators I would like to test with that method. The Method should be executed as fast as possible, for now I'm quite happy with the performance of Parallel.For (I would love to learn about faster/better methods).

I need to generate a new object for each Thread and the current # of Thread for my EvalEnumeration(int instance) method. Several attempts have failed because of 开发者_Python百科these constraints.

Some of my tries:


StartNewTest(new PSTLooper(intHands));

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

that approach compiles, but only uses the IEvaluator and does not create a new one.


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

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

that does not compile, as I need the # of Instance.


I'm quite sure that my approach is not the best, but for now I don't know any better and thus need to ask this question here.


Does this work for you?

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜