title: My code should go 18 times faster but is only 5 times faster
body: <p>I have tried to really understand this question I am going to ask now for a year. Trying to google it, read about multicore computing, parallel.foreach approach, ask questions about this. But never got a clear answer for what is going on. I always suspect no one truly knows and just guessing.</p>
  
  <p><strong>The Question:</strong></p>
  
  <p><strong>My computer has: 24 cores.<br />
  I have a function: calculationFunction()</strong></p>
  
  <p><em>It is possible to test this code</em></p>
  
  <p><strong>Now to the benchmark which is my BIG question:</strong><br />
  If I run calculationFunction() with 1 core. It takes 42 seconds<br />
  If I run calculationFunction() with 18 cores. It takes 11 seconds<br /></p>
  
  <p>Now is my question why doesn't it go faster than 11 seconds. If 1 core takes 42 seconds. Shouldn't 18 cores take: <strong>42/18 = Around or close to 2.33 seconds?</strong></p>
  
  <p>What is truly the problem here. Is there a bottleneck somewhere or doesn't actually 18 cores get involved in this process. Something is not right?</p>
  
  <p><strong>IMPORTANT TO MENTION:</strong><br />
  I know from other exact same tests. I have opened 18 instances of the same application and split a work into 18 pieces and this did go exactly 18 times faster.</p>
  
  <p>So is there something that restricts ONE instance of an application to NOT use all 18 cores that I assign in this code?</p>
  
  <p>It is possible to test this code:</p>
  
  <pre><code>public void runThreads()
  {
      //Change this variable to make tests
      int nrCores = 18; 
  
      List&lt;List&lt;String&gt;&gt; minusLIST2D = new List&lt;List&lt;String&gt;&gt;(); 
      List&lt;List&lt;String&gt;&gt; plusLIST2D = new List&lt;List&lt;String&gt;&gt;();                
      int nrloops = 3000000;
      nrloops = nrloops / nrCores;
  
      /*---------------------------------------------------------------*/
      var stopwath = new Stopwatch(); 
      stopwath.Start();
      Task[] tasks = new Task[nrCores];
      for (int i = 0; i &lt; nrCores; i++)
      {
          //Add lists
          minusLIST2D.Add(new List&lt;String&gt;());
          plusLIST2D.Add(new List&lt;String&gt;());
  
          //Start Task
          int index = i;
          tasks[index] = Task.Factory.StartNew(() =&gt; calculationFunction(nrloops, minusLIST2D[index], plusLIST2D[index]));
      }
      Task.WaitAll(tasks); //Wait for all Tasks to complete
      stopwath.Stop(); 
      MessageBox.Show("Elapsec secs: " + stopwath.Elapsed.TotalSeconds.ToString());
  }
  
  public void calculationFunction(int nrloops, List&lt;String&gt; minusLIST, List&lt;String&gt; plusLIST)
  {
      System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
  
      double num1 = 0; double num2 = 0; double num3 = 0; double number1 = 0; double number2 = 0; double number3 = 0; double thenum1 = 0; double thenum2 = 0; double thenum3 = 0;
      double NUM1 = 0; double NUM2 = 0; double NUM3 = 0; double NUMBER1 = 0; double NUMBER2 = 0; double NUMBER3 = 0; String str = ""; String num11 = ""; String num22 = ""; String num33 = "";
      String number11 = ""; String number22 = ""; String number33 = ""; double calc1 = 0;
  
      for (int i = 0; i &lt; nrloops; i++)
      {
          //Calculate both calc scenarios!
          num1 = 12.3; number1 = 13.3; thenum1 = 14.3;
          num2 = 12.3; number2 = 13.3; thenum2 = 14.3;
          num3 = 12.3; number3 = 13.3; thenum3 = 14.3;
  
          NUM1 = num1; NUM2 = num2; NUM3 = num3; NUMBER1 = number1; NUMBER2 = number2; NUMBER3 = number3;
          if (num1 &lt;= 0 || number1 &lt;= 0) { NUM1 = thenum1; NUMBER1 = thenum1; }
          if (num2 &lt;= 0 || number2 &lt;= 0) { NUM2 = thenum2; NUMBER2 = thenum2; }
          if (num3 &lt;= 0 || number3 &lt;= 0) { NUM3 = thenum3; NUMBER3 = thenum3; }
          if (NUM1 &gt; 0 &amp;&amp; NUM2 &gt; 0 &amp;&amp; NUM3 &gt; 0 &amp;&amp; NUMBER1 &gt; 0 &amp;&amp; NUMBER2 &gt; 0 &amp;&amp; NUMBER3 &gt; 0)
          {
              str = ""; num11 = ""; num22 = ""; num33 = ""; number11 = ""; number22 = ""; number33 = "";
              if (num1 &gt; 0 &amp;&amp; num2 &gt; 0 &amp;&amp; num3 &gt; 0 &amp;&amp; number1 &gt; 0 &amp;&amp; number2 &gt; 0 &amp;&amp; number3 &gt; 0) { } 
              else { str = string.Format("{0:F10}", thenum1) + " / " + string.Format("{0:F10}", thenum2) + " / " + string.Format("{0:F10}", thenum3); }
  
              if (num1 &lt;= 0) { num11 = "0"; num1 = thenum1; } else { num11 = string.Format("{0:F10}", num1); }
              if (num2 &lt;= 0) { num22 = "0"; num2 = thenum2; } else { num22 = string.Format("{0:F10}", num2); }
              if (num3 &lt;= 0) { num33 = "0"; num3 = thenum3; } else { num33 = string.Format("{0:F10}", num3); }
              if (number1 &lt;= 0) { number11 = "0"; number1 = thenum1; } else { number11 = string.Format("{0:F10}", number1); }
              if (number2 &lt;= 0) { number22 = "0"; number2 = thenum2; } else { number22 = string.Format("{0:F10}", number2); }
              if (number3 &lt;= 0) { number33 = "0"; number3 = thenum3; } else { number33 = string.Format("{0:F10}", number3); }
  
              //Calculate
              calc1 = ((num1 * number2 * number3) - 45) / 10;
  
              //String
              str = calc1 + "," + "ab" + " - " + "ab" + " - " + "ab" + "," +
                    "ab" + " - " + "ab" + " - " + "ab" + "," +
                    num11 + " / " + num22 + " / " + num33 + "," +
                    number11 + " / " + number22 + " / " + number33 + "," +
                    str + "," +
                    calc1 + "%";
  
              if (calc1 &gt; 0)
              {
                  plusLIST.Add(str);
              }
              else
              {
                  minusLIST.Add(str);
              }
          }
      }
  }
  </code></pre>