MaxDegreeOfParallelism = Environment.ProcessorCount slows down runtime on my CPU

I have the following program (which I got from http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx ) that splits a task using Parallel.For loop

class Program { static void Main(string[] args) { var watch = Stopwatch.StartNew(); Parallel.For(2, 20, (i) => { var result = SumRootN(i); Console.WriteLine("root {0} : {1} ", i, result); }); Console.WriteLine(watch.ElapsedMilliseconds); Console.ReadLine(); } public static double SumRootN(int root) { double result = 0; for (int i = 1; i < 10000000; i++) { result += Math.Exp(Math.Log(i) / root); } return result; } } 

When I run this test several times, I get the time:

1992, 2140, 1783, 1863 ms, etc. etc.

My first question is: why are there always different times? I do the same calculation every time, but time changes every time.

Now, when I add the following code to use all available processors on my processor:

  var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount (On my CPU this is 8) }; Parallel.For(2, 20, parallelOptions, (i) => { var result = SumRootN(i); Console.WriteLine("root {0} : {1} ", i, result); }); 

I notice that the execution time is actually increasing! Time:

2192, 3192, 2603, 2245 ms, etc. etc.

Why does this increase time? Am I using this incorrectly?

+6
source share
1 answer

From http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism(v=vs.110).aspx

By default, For and ForEach will use as many threads as the main scheduler provides. Changing MaxDegreeOfParallelism by default limits the number of simultaneous tasks.

This means that setting MaxDegreeOfParallelism to the number of processors actually limits the bandwidth of the Parallel.For loop to use the optimal number of threads for the workload. For example, I have a transfer task that uses about 60 threads for about 600 iterations of a long code, much more than 1 thread per processor limit that you are trying to set.

MaxDegreeOfParallelism or ThreadPool.SetMaxThreads should be used only if you clearly need to prevent a certain number of threads from executing. For example, if you are using an Access database, I would set it to 64, since this is the maximum number of concurrent connections that Access can process on a single process.

+17
source

All Articles