Question

I'm running 200 tasks of math with parallel.foreach. What is the most time efficient way to do it?

List<Task<double>> actions = new List<Task<double>>(200)
{
  Task<Double>.Factory.StartNew(() =>Tree.calc0(features)),
  Task<Double>.Factory.StartNew(() =>Tree.calc1(features)),
..................
...................
}


 Parallel.ForEach(actions, (action) => { result += action.Result; });

I want it to run no more than 0.5 second. current performance-> 2.5 seconds Thanks!

Était-ce utile?

La solution

Factory.StartNew is not only creating the task, but it is also running it.

You don't even need to create a task, just invoke calc inside the ForEach loop.

You actually need to create the task without running it

Task<double> x = new Task<double>(() => Tree.calc0(features));

And then start the action on the Parallel.ForEach.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top