title: Eigen::Matrix vs. boost::multi_array vs. Eigen::Map
body: <p>I'm getting puzzling results while doing fairly simple tasks to compare the performance of:</p>
  <ul>
  <li><code>Eigen::Matrix</code></li>
  <li><code>boost::multi_array</code></li>
  <li><code>boost::multi_array</code> mapped to <code>Eigen::Matrix</code> using <code>Eigen::Map</code></li>
  </ul>
  <p>This is an abridged version of my test code; a fuller version can be found at: <a href="http://pastebin.com/faZ7TvJG" rel="nofollow noreferrer">http://pastebin.com/faZ7TvJG</a>.</p>
  <pre><code>boost::multi_array&lt;double, 2, Eigen::aligned_allocator&lt;double&gt; &gt; boost_multi_array;
  Eigen::Matrix&lt;double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor&gt; eigen_matrix;
  Eigen::Map&lt;Eigen::Matrix&lt;double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor&gt; &gt; boost_multi_array_mapped(boost_multi_array.data(), rows, cols);
  
  double
      tmp_sum = 0,
      tmp_time = omp_get_wtime();
  
  for(size_t i=0; i&lt;iterations; i++)
  {
      for(size_t j=0; j&lt;rows; j++)
      {
          for(size_t k=0; k&lt;cols; k++)
          {
              //if(k%2==0)
              //{ // commented out are the different options
                  //tmp_sum += boost_multi_array[j][k];
                  //tmp_sum += boost_multi_array_mapped(j,k);
                  tmp_sum += eigen_matrix(j,k);
              //}
          }
      }
  }
  
  const double sequential_access_time = omp_get_wtime() - tmp_time;
  </code></pre>
  <p>The results are as follows:</p>
  <pre><code>Sequential Access:
     BOOST (MAPPED)   : 1.45763s
     EIGEN            : 1.45736s
     BOOST            : 2.58971s
  </code></pre>
  <p>If I use an if-statement to skip every second element, I then get the following results:</p>
  <pre><code>Alternating Access:
     BOOST (MAPPED)   : 1.67301s
     EIGEN            : 2.08834s
     BOOST            : 2.35295s
  </code></pre>
  <p>Inspecting the assembly shows that in the sequential access case, Eigen is faster because the sum becomes vectorized, while it does not when using raw boost::multi_array.</p>
  <p>My questions then are:</p>
  <ol>
  <li>Why is boost::multi_array not vectorized, while Eigen::Matrix is?</li>
  <li>Why would a multi_array mapped to Eigen be faster than a &quot;native&quot; Eigen data structure?</li>
  </ol>
  <p>For compilation I use the following:</p>
  <pre><code>g++ -I /usr/include/eigen3 test.cpp -Wall -O3 -DNDEBUG -DBOOST_DISABLE_ASSERTS -fopenmp -ffast-math -funroll-loops -march=native -mtune=native -o array_test
  </code></pre>