12,828,639 members (46,041 online)
Rate this:
See more:
How do I save the results of the method called in the parallel loop that returns a double result to a list? Each time the method is run it returns a result. I want to add that specific result to a list.

```Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10; i++)
{
SumRootN(i,"t");
}
sw.Stop();

Stopwatch sw2 = new Stopwatch();
sw2.Start();

//NEED TO SAVE RESULTS TO LIST
Parallel.For(0, 10, i => SumRootN(i,"t"));

sw2.Stop();
double t = sw2.ElapsedTicks;
Console.WriteLine("Seq " + sw.ElapsedMilliseconds);
Console.WriteLine("Parallel " + sw2.ElapsedMilliseconds);
```

```public static double SumRootN(int root,string test)
{

double result = 0;
for (int i = 1; i < 1000000; i++)
{
result += Math.Exp(Math.Log(i) / root);

}
return result;
}```
Posted 5-Dec-12 3:03am
Updated 5-Dec-12 5:27am
v2

Rate this:

## Solution 1

I'm not entirely sure if this is exactly what you need, but Parallel Aggregation[^] might be your solution.
Rate this:

## Solution 3

shekarchee is close, but if you need to keep the results in the same sequence as if it was NOT performed in parallel, then the simplest is to use an array instead of the `List<double>`.
```const int count = 10;
double[] results = new double[count];
Parallel.For(0, count, i => { results[i] = SumRootN(i, "t"); });```

Top Experts
Last 24hrsThis month
 Jochen Arndt 86 F-ES Sitecore 80 OriginalGriff 75 ppolymorphe 60 Dave Kreskowiak 55
 OriginalGriff 5,672 Graeme_Grant 3,935 Karthik Bangalore 3,706 Jochen Arndt 2,825 ppolymorphe 2,799