title: How effective is multi-threading vs sequential processing?
body: <p>I have a unit of work on a .NET 4.51, C# Web Service that takes 100 milliseconds.  Usually the web request contains 10 units of work or so.  Thus processing it sequentially via a for loop takes about a second.  </p>
  
  <pre><code>foreach (var u in unitsOfWork) {
    Run(u);
  }
  </code></pre>
  
  <p>Since the box has 12 CPUs, I decided to split up the work and run it in parallel hoping to get a performance gain.  I used <code>Parallel.ForEach</code> to do the work:</p>
  
  <pre><code>Parallel.ForEach(unitsOfWork,u =&gt; {
      Run(u);
  });    
  </code></pre>
  
  <p>To my surprise, each unit of work took on average 425 milliseconds.  So in the end I saved about 500 milliseconds off the request.  It seems like I should be able to get better performance seeing how the box has 12 CPUs...  Am I missing something simple?</p>
  
  <p>I looked for anything that is shared (that could be holding it up), but found nothing...so I tried to experiment.  I sent a request with 2 units of work and each took about 125 ms.  With 3 requests each unit took 150 ms and so on.  With each subsequent number of units, there was a penalty of around 25 to 30 ms.</p>
  
  <p>So either I am doing something wrong... or there is just inherent overhead to multi-threading (didn't realize it was this big).</p>
  
  <p>P.S. I also tried replacing Parallel.For with Thread.Join - same results.</p>