I have the following C# code fragment:
using System;
class count {
public static void Main()
{
int [] a = {-30, 30, -20, -10, 40, 0, 10, 5};
int i,j,k;
int N=8;
for (i=0; i < N; ++i)
for (j=i+1; j < N; ++j)
for (k=j+1; k < N; ++k)
if (a[i] + a[j] + a[k] == 30)
Console.WriteLine (a[i].ToString () + a[j].ToString() + a[k].ToString());
}
}
What the above program does is, find out the triplets a1, a2, a3 from an Array A, such that the sum of the triplets is 30.
I want to know how I can do the sum calculation using the C# Parallel.For extension.
I know that this is used as an interview question and there are better alternative algorithms than an i,j,k loop. However, all I want is just to understand how to perform this using Parallel extensions for C# , in an effective way.
dan’s answer will work and is a correct way to use
Parallel.For, but I went through the trouble of profiling the code and I think you’ll find that parallelising this won’t make it any faster. EachParellel.Formakes several new threads, usually more than three, so with 3 nestedParellel.Fors you will have have at least 3^3 (27) threads, which is more way more than the number of logical processors on any machine. The extra threads will if anything add an overhead and slow it down.So why not just have one
Parallel.Forand 2 ordinaryforloops – this will mean that there are around 3-4 threads which will work great on a dual or quad core machine. Like this method:Take the following code used to profile both methods:
This yields these results for average execution time for each method: (in Release mode, using .Net 4.0, on a quad core Intel Core i5 machine):