title: C# Parallel Vs. Threaded code performance
body: <p>I've been testing the performance of System.Threading.Parallel vs a Threading and I'm surprised to see Parallel taking longer to finish tasks than threading. I'm sure it's due to my limited knowledge of Parallel, which I just started reading up on.</p>
  
  <p>I thought i'll share few snippets and if anyone can point out to me paralle code is running slower vs threaded code. Also tried to run the same comparison for finding prime numbers and found parallel code finishing much later than threaded code.</p>
  
  <pre><code>public class ThreadFactory
  {
      int workersCount;
      private List&lt;Thread&gt; threads = new List&lt;Thread&gt;();
  
      public ThreadFactory(int threadCount, int workCount, Action&lt;int, int, string&gt; action)
      {
          workersCount = threadCount;
  
          int totalWorkLoad = workCount;
          int workLoad = totalWorkLoad / workersCount;
          int extraLoad = totalWorkLoad % workersCount;
  
          for (int i = 0; i &lt; workersCount; i++)
          {
              int min, max;
              if (i &lt; (workersCount - 1))
              {
                  min = (i * workLoad);
                  max = ((i * workLoad) + workLoad - 1);
              }
              else
              {
                  min = (i * workLoad);
                  max = (i * workLoad) + (workLoad - 1 + extraLoad);
              }
              string name = "Working Thread#" + i; 
  
              Thread worker = new Thread(() =&gt; { action(min, max, name); });
              worker.Name = name;
              threads.Add(worker);
          }
      }
  
      public void StartWorking()
      {
          foreach (Thread thread in threads)
          {
              thread.Start();
          }
  
          foreach (Thread thread in threads)
          {
              thread.Join();
          }
      }
  }
  </code></pre>
  
  <p>Here is the program:</p>
  
  <pre><code>Stopwatch watch = new Stopwatch();
  watch.Start();
  int path = 1;
  
  List&lt;int&gt; numbers = new List&lt;int&gt;(Enumerable.Range(0, 10000));
  
  if (path == 1)
  {
      Parallel.ForEach(numbers, x =&gt;
      {
          Console.WriteLine(x);
          Thread.Sleep(1);
  
      });
  }
  else
  {
      ThreadFactory workers = new ThreadFactory(10, numbers.Count, (min, max, text) =&gt; {
  
          for (int i = min; i &lt;= max; i++)
          {
              Console.WriteLine(numbers[i]);
              Thread.Sleep(1);
          }
      });
  
      workers.StartWorking();
  }
  
  watch.Stop();
  Console.WriteLine(watch.Elapsed.TotalSeconds.ToString());
  
  Console.ReadLine();
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>Taking Locking into consideration: I tried the following snippet. Again the same results, Parallel seems to finish much slower.</p>
  
  <p>path = 1;
          cieling = 10000000;</p>
  
  <pre><code>    List&lt;int&gt; numbers = new List&lt;int&gt;();
  
      if (path == 1)
      {
          Parallel.For(0, cieling, x =&gt;
          {
              lock (numbers)
              {
                  numbers.Add(x);    
              }
  
          });
      }
  
      else
      {
          ThreadFactory workers = new ThreadFactory(10, cieling, (min, max, text) =&gt;
          {
  
              for (int i = min; i &lt;= max; i++)
              {
                  lock (numbers)
                  {
                      numbers.Add(i);    
                  }                       
  
              }
          });
  
          workers.StartWorking();
      }
  </code></pre>
  
  <p><strong>Update 2:</strong>
  Just a quick update that my machine has Quad Core Processor. So Parallel have 4 cores available.</p>