title: Parallel on C++ isn't fast like C#
body: <p>I have code like this on <strong>C#</strong></p>
  <pre><code>private static Random random = new Random();
  public static string RandomString(int length)
  {
      const string chars = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;;
      return new string(Enumerable.Repeat(chars, length)
        .Select(s =&gt; s[random.Next(s.Length)]).ToArray());
  }
       Task.Factory.StartNew(() =&gt; {       
                   System.Threading.Tasks.Parallel.For(0L, 10000000, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 10 }, n =&gt;
                   {
                   Console.WirteLine(RandomString(12));
                   });
                  }
  </code></pre>
  <p>Add parallel method to it, it manage to run generate 10 millions random string in less than 8 seconds &amp; use all CPU power</p>
  <p><a href="https://i.stack.imgur.com/e0FLv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e0FLv.jpg" alt="enter image description here" /></a></p>
  <p>I tried to do it again in <strong>C++</strong></p>
  <pre><code>string NonRepeatChar(int max_len)
  {
      std::string valid_chars = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&quot;;
  
      std::random_device rd;
      std::mt19937 g(rd());
      std::shuffle(valid_chars.begin(), valid_chars.end(), g);
      std::string rand_str(valid_chars.begin(), valid_chars.begin() + max_len);
      return rand_str;
  }
  </code></pre>
  <p>Applied the code to some recommend <strong>C++</strong> parallel method</p>
  <pre><code>void multiply()
  {
  for (size_t i = 0; i &lt; 10; i++)
  {
      for (size_t j = 0; j &lt; 10; j++)
      {           
          for (int k = 0; k &lt; 10; k++)
          {
              printf(&quot;%s\n&quot;,NonRepeatChar(10));
          }           
      }
  }
  }
  
  class Foo {
  public:
      Foo() : counter_(0) {}
  
      std::pair&lt;string, std::future&lt;void&gt;&gt; a_function(std::future&lt;void&gt;&amp; f) {
          // Ensure that the background task from the previous iteration
          // has completed
          f.wait();
  
          // Set the task for the next iteration
          std::future&lt;void&gt; fut = std::async(std::launch::async, &amp;Foo::background_task, this);
  
          // Do some work
          string value = NonRepeatChar(12);
  
          // Return the result and the future for the next iteration
          return std::make_pair(value.c_str(), std::move(fut));
      }
  
      void background_task() {
          ++counter_;
      }
  
  private:
      std::atomic&lt;int&gt; counter_;
  };
  </code></pre>
  <p>Record the time when run it</p>
  <pre><code>int main()
  {   
      clock_t tStart = clock();
      std::future&lt;void&gt; bleak = std::async(std::launch::deferred, []() {});
  
      Foo foo;
  
      for (size_t i = 0; i &lt; 10000; ++i) {
          // Call the function
          std::pair&lt;string, std::future&lt;void&gt;&gt; result = foo.a_function(bleak);    
          bleak = std::move(result.second);   
          std::cout &lt;&lt; result.first &lt;&lt; &quot;\n&quot;;
      }
      printf(&quot;Time taken: %.2fs\n&quot;, (double)(clock() - tStart) / CLOCKS_PER_SEC);
      return 0;
      }
  </code></pre>
  <p>Here're my result:</p>
  <blockquote>
  <p>10.98s//normal loop</p>
  <p>8.76s//multiply</p>
  <p>8.88s//Foo</p>
  </blockquote>
  <p>Obviously the code didn't have any difference compare to original loop, only generate 10000 line, &amp; it didn't even use all CPU power like <strong>C#</strong>. Is there something wrong with the parallel method? How may I optimize it?</p>