Note that there are some explanatory texts on larger screens.

plurals
  1. POMaxDegreeOfParallelism = Environment.ProcessorCount slows down execution time on my CPU
    primarykey
    data
    text
    <p>I have the following program (that I got from <a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx" rel="nofollow">http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx</a>) that splits a task up using <strong>Parallel.For</strong> loop</p> <pre><code>class Program { static void Main(string[] args) { var watch = Stopwatch.StartNew(); Parallel.For(2, 20, (i) =&gt; { 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 &lt; 10000000; i++) { result += Math.Exp(Math.Log(i) / root); } return result; } } </code></pre> <p>When I run this test several times I get times of: </p> <p>1992, 2140, 1783, 1863 ms etc etc.</p> <p>My first question is, <strong>why are the times always different??</strong> I am doing the exact same calculations each time yet the times vary each time.</p> <p>Now when I add in the following code to make use of all the available processors on my CPU:</p> <pre><code> var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount (On my CPU this is 8) }; Parallel.For(2, 20, parallelOptions, (i) =&gt; { var result = SumRootN(i); Console.WriteLine("root {0} : {1} ", i, result); }); </code></pre> <p>I notice that the execution times actually increase!! The times are now:</p> <p>2192, 3192, 2603, 2245 ms etc etc.</p> <p><strong>Why does this cause the times to increase? Am I using this wrong?</strong> </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload