title: Efficiency of STL algorithms with fixed size arrays
body: <p>In general, I assume that the STL implementation of any algorithm is at least as efficient as anything I can come up with (with the additional benefit of being error free). However, I came to wonder whether the STL's focus on iterators might be harmful in some situations.</p>
  
  <p>Lets assume I want to calculate the inner product of two fixed size arrays. My naive implementation would look like this:</p>
  
  <pre><code>std::array&lt;double, 100000&gt; v1;
  std::array&lt;double, 100000&gt; v2;
  //fill with arbitrary numbers
  
  double sum = 0.0;
  for (size_t i = 0; i &lt; v1.size(); ++i) {
      sum += v1[i] * v2[i];
  }
  </code></pre>
  
  <p>As the number of iterations and the memory layout are known during compile time and all operations can directly be mapped to native processor instructions, the compiler should easily be able to generate the "optimal" machine code from this (loop unrolling, vectorization / FMA instructions ...).</p>
  
  <p>The STL version</p>
  
  <pre><code>double sum = std::inner_product(cbegin(v1), cend(v1), cbegin(v2), 0.0);
  </code></pre>
  
  <p>on the other hand adds some additional indirections and even if everything is inlined, the compiler still has to deduce that it is working on a continuous memory region and where this region lies. While this is certainly possible in principle, I wonder, whether the typical c++ compiler will actually do it.</p>
  
  <p><strong>So my question is</strong>: Do you think, there can be a performance benefit of implementing standard algorithms that operate on fixed size arrays on my own, or will the STL Version always outperform a manual implementation?</p>