title: Parallel version of the `std::generate` performs worse than the sequential one
body: <p>I'm trying to parallelize some old code using the <a href="https://en.cppreference.com/w/cpp/header/execution" rel="nofollow noreferrer">Execution Policy</a> from the C++ 17. My sample code is below:</p>
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;chrono&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;execution&gt;
  #include &lt;vector&gt;
  
  using Clock = std::chrono::high_resolution_clock;
  using Duration = std::chrono::duration&lt;double&gt;;
  
  constexpr auto NUM = 100'000'000U;
  
  double func()
  {
    return rand();
  }
  
  int main()
  {
    std::vector&lt;double&gt; v(NUM);
    // ------ feature testing
    std::cout &lt;&lt; &quot;__cpp_lib_execution         : &quot; &lt;&lt; __cpp_lib_execution &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;__cpp_lib_parallel_algorithm: &quot; &lt;&lt; __cpp_lib_parallel_algorithm &lt;&lt; std::endl;
    // ------ fill the vector with random numbers sequentially
    auto const startTime1 = Clock::now();
    std::generate(std::execution::seq, v.begin(), v.end(), func);
    Duration const elapsed1 = Clock::now() - startTime1;
    std::cout &lt;&lt; &quot;std::execution::seq: &quot; &lt;&lt; elapsed1.count() &lt;&lt; &quot; sec.&quot; &lt;&lt; std::endl;
    // ------ fill the vector with random numbers in parallel
    auto const startTime2 = Clock::now();
    std::generate(std::execution::par, v.begin(), v.end(), func);
    Duration const elapsed2 = Clock::now() - startTime2;
    std::cout &lt;&lt; &quot;std::execution::par: &quot; &lt;&lt; elapsed2.count() &lt;&lt; &quot; sec.&quot; &lt;&lt; std::endl;
  }
  </code></pre>
  <p>The program output on my Linux desktop:</p>
  <pre><code>__cpp_lib_execution         : 201902
  __cpp_lib_parallel_algorithm: 201603
  std::execution::seq: 0.971162 sec.
  std::execution::par: 25.0349 sec.
  </code></pre>
  <p>Why does the parallel version performs 25 times worse than the sequential one?</p>
  <p>Compiler: g++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0</p>