title: How to use Armadillo Columns/Rows to perform optimised calculations on accesses within the same column
body: <p>What is the best way to manipulate indexing in Armadillo? I was under the impression that it heavily used template expressions to avoid temporaries, but I'm not seeing these speedups.</p>
  
  <p>Is direct array indexing still the best way to approach calculations that rely on consecutive elements within the same array?</p>
  
  <p>Keep in mind, that I hope to parallelise these calculations in the future with TBB::parallel_for (In this case, from a maintainability perspective, it may be simpler to use direct accessing?) These calculations happen in a tight loop, and I hope to make them as optimal as possible. </p>
  
  <pre><code>   ElapsedTimer timer;
     int n = 768000;
     int numberOfLoops = 5000;
     arma::Col&lt;double&gt; directAccess1(n);
     arma::Col&lt;double&gt; directAccess2(n);
     arma::Col&lt;double&gt; directAccessResult1(n);
     arma::Col&lt;double&gt; directAccessResult2(n);
     arma::Col&lt;double&gt; armaAccess1(n);
     arma::Col&lt;double&gt; armaAccess2(n);
     arma::Col&lt;double&gt; armaAccessResult1(n);
     arma::Col&lt;double&gt; armaAccessResult2(n);
     std::valarray&lt;double&gt; valArrayAccess1(n);
     std::valarray&lt;double&gt; valArrayAccess2(n);
     std::valarray&lt;double&gt; valArrayAccessResult1(n);
     std::valarray&lt;double&gt; valArrayAccessResult2(n);
     // Prefil
     for (int i = 0; i &lt; n; i++) {
        directAccess1[i] = i;
        directAccess2[i] = n - i;
        armaAccess1[i] = i;
        armaAccess2[i] = n - i;
        valArrayAccess1[i] = i;
        valArrayAccess2[i] = n - i;
     }
     timer.Start();
     for (int j = 0; j &lt; numberOfLoops; j++) {
        for (int i = 1; i &lt; n; i++) {
           directAccessResult1[i] = -directAccess1[i] / (directAccess1[i] + directAccess1[i - 1]) *  directAccess2[i - 1];
           directAccessResult2[i] = -directAccess1[i] / (directAccess1[i] + directAccess1[i]) *  directAccess2[i];
        }
     }
     timer.StopAndPrint("Direct Array Indexing Took");
     std::cout &lt;&lt; std::endl;
  
     timer.Start();
     for (int j = 0; j &lt; numberOfLoops; j++) {
        armaAccessResult1.rows(1, n - 1) = -armaAccess1.rows(1, n - 1) / (armaAccess1.rows(1, n - 1) + armaAccess1.rows(0, n - 2)) % armaAccess2.rows(0, n - 2);
        armaAccessResult2.rows(1, n - 1) = -armaAccess1.rows(1, n - 1) / (armaAccess1.rows(1, n - 1) + armaAccess1.rows(1, n - 1)) % armaAccess2.rows(1, n - 1);
     }
     timer.StopAndPrint("Arma Array Indexing Took");
     std::cout &lt;&lt; std::endl;
  
     timer.Start();
     for (int j = 0; j &lt; numberOfLoops; j++) {
        for (int i = 1; i &lt; n; i++) {
           valArrayAccessResult1[i] = -valArrayAccess1[i] / (valArrayAccess1[i] + valArrayAccess1[i - 1]) *  valArrayAccess2[i - 1];
           valArrayAccessResult2[i] = -valArrayAccess1[i] / (valArrayAccess1[i] + valArrayAccess1[i]) *  valArrayAccess2[i];
        }
     }
     timer.StopAndPrint("Valarray Array Indexing Took:");
     std::cout &lt;&lt; std::endl;
  </code></pre>
  
  <p>In vs release mode (/02 - to avoid armadillo array indexing checks), they produce the following timings:</p>
  
  <pre><code>Started Performance Analysis!
  Direct Array Indexing Took: 37.294 seconds elapsed
  Arma Array Indexing Took: 39.4292 seconds elapsed
  Valarray Array Indexing Took:: 37.2354 seconds elapsed
  </code></pre>