title: Eigen expression template slower than manual loop for exponential
body: <p>I'm trying to compute a sum of component-wise exponentials of an array. The array is an <code>Eigen::ArrayXd</code>, so I expect that Eigen's <code>exp</code> function will be optimized to the same code, or better, than a manual loop. Instead, I'm seeing that a manual loop is tens of percents faster. (In the example below, roughly 2.1 seconds for Eigen and 1.6 seconds for a manual loop.)</p>
  
  <p>I'm not using any Eigen vectorization (SSE is disabled), MKL, or anything else special. This is just a default Visual Studio 2010 project, on the Release configuration, with Eigen 3.2.9 out-of-the-box. Specifying "Full Optimization" (/Ox) and "Favor fast code" (/Ot) makes no difference to the results.</p>
  
  <p>I'm not knowledgeable enough to look at compiled code to understand what's happening - could anyone suggest why using Eigen directly might be slower, and how to coax it to produce the same performance as a manual loop?</p>
  
  <pre><code>#include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;Eigen/Dense&gt;
  
  int main(int argc, char* argv[])
  {
      const Eigen::ArrayXd xs(Eigen::ArrayXd::Random(1000000));
      Eigen::ArrayXd array_result(Eigen::ArrayXd::Zero(xs.size())), loop_result(Eigen::ArrayXd::Zero(xs.size()));
  
      {
          std::clock_t start_time = std::clock();
          for (int i = 1; i &lt;= 100; ++i) {
              const double b = i; // Values not important;
              array_result += exp(b * xs);
          }
          std::clock_t end_time = std::clock();
          std::cout &lt;&lt; "Array time (seconds): " &lt;&lt; static_cast&lt;double&gt;(end_time - start_time) / CLOCKS_PER_SEC &lt;&lt; std::endl;
      }
  
      {
          std::clock_t start_time = std::clock();
          for (int i = 1; i &lt;= 100; ++i) {
              const double b = i; // Values not important;
              for (Eigen::ArrayXd::Index i = 0; i &lt; xs.size(); ++i) {
                  loop_result[i] += exp(b * xs[i]);
              }
          }
          std::clock_t end_time = std::clock();
          std::cout &lt;&lt; "Loop time (seconds): " &lt;&lt; static_cast&lt;double&gt;(end_time - start_time) / CLOCKS_PER_SEC &lt;&lt; std::endl;
      }
  
      system("pause");
      return 0;
  }
  </code></pre>