Hello all,<br><br>I&#39;ve recently started looking into the speed gains from using Rcpp and have began looking into the arma library too. Ive written a small loop to fill in a matrix and done so using the arma::mat class and the Rcpp::NumericMatrix class. In this case, the Rcpp::NumericMatrix class was slightly faster. Is this usually the case, or have I written quite inefficient code in some parts? It&#39;s my understanding that the advantage of the arma::mat is that is has more methods associated with it, is this correct?<br>
<br>I&#39;ve also added an R function to do this and as per usual, the speed gain of simply using Rcpp is incredible!<br><br>Any input massively appreciated<br><br>Jeff<br><br>library(RcppArmadillo)<br>library(inline)<br>
library(rbenchmark)<br><br># start with R function....<br><br>fillMiddleTheta &lt;- function(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem) {<br>    alphaCounter &lt;- alphaCounterStart<br>
    for (i in (thetaStartIndex + 1L):(thetaEndIndex - 1L)) {<br>        addition &lt;- exp(alphas[alphaCounter] + alphas[alphaCounter + 1L] * timerem)<br>        thetaMatrix[ , i] &lt;- thetaMatrix[ , i - 1L] + addition<br>
        alphaCounter &lt;- alphaCounter + 2L<br>    }<br>    return(thetaMatrix)<br>}<br><br># cpp function....<br><br>src &lt;- &#39;<br>        Rcpp::NumericMatrix thetaMatrix(sthetaMatrix);<br>        <br>        Rcpp::NumericVector alphas(salphas);<br>
        Rcpp::NumericVector timerem(stimerem);<br>        <br>        int thetaStartIndex = as&lt;int&gt;(sthetaStartIndex);<br>        int thetaEndIndex = as&lt;int&gt;(sthetaEndIndex);<br>        int alphaCounterStart = as&lt;int&gt;(salphaCounterStart);<br>
        <br>        int alphaCounter = alphaCounterStart;<br>        <br>        for (int i = thetaStartIndex + 1; i &lt;= thetaEndIndex - 1; i++) {<br>        <br>        thetaMatrix(_, i - 1) = thetaMatrix(_, i - 2) + exp(alphas(alphaCounter - 1) + alphas(alphaCounter) * timerem);<br>
        alphaCounter = alphaCounter + 2;<br>        <br>        }<br>        <br>        return wrap(thetaMatrix);<br>        &#39;<br><br>cppFillMiddleTheta &lt;- cxxfunction(signature(sthetaMatrix = &quot;Matrix&quot;, sthetaStartIndex = &quot;integer&quot;, <br>
                sthetaEndIndex = &quot;integer&quot;, salphas = &quot;numericVector&quot;, salphaCounterStart = &quot;integer&quot;, stimerem = &quot;numericVector&quot;), <br>        src, &quot;Rcpp&quot;)<br><br># cpp function with armadillo....<br>
<br>armaSrc &lt;- &#39;<br>        arma::mat thetaMatrix = Rcpp::as&lt;arma::mat&gt;(sthetaMatrix);<br>        <br>        arma::colvec alphas = Rcpp::as&lt;arma::colvec&gt;(salphas);<br>        arma::colvec timerem = Rcpp::as&lt;arma::colvec&gt;(stimerem);<br>
        <br>        int thetaStartIndex = as&lt;int&gt;(sthetaStartIndex);<br>        int thetaEndIndex = as&lt;int&gt;(sthetaEndIndex);<br>        int alphaCounterStart = as&lt;int&gt;(salphaCounterStart);<br>        <br>
        int alphaCounter = alphaCounterStart;<br>        <br>        for (int i = thetaStartIndex + 1; i &lt;= thetaEndIndex - 1; i++) {<br>        <br>        thetaMatrix.col(i - 1) = thetaMatrix.col(i - 2) + exp(alphas(alphaCounter - 1) + alphas(alphaCounter) * timerem);<br>
        alphaCounter = alphaCounter + 2;<br>        <br>        }<br>        <br>        return wrap(thetaMatrix);<br>        &#39;<br><br>cppArmaFillMiddleTheta &lt;- cxxfunction(signature(sthetaMatrix = &quot;Matrix&quot;, sthetaStartIndex = &quot;integer&quot;, <br>
                sthetaEndIndex = &quot;integer&quot;, salphas = &quot;numericVector&quot;, salphaCounterStart = &quot;integer&quot;, stimerem = &quot;numericVector&quot;), <br>        armaSrc, &quot;RcppArmadillo&quot;)<br>
<br># test<br>n &lt;- 1e6<br><br>thetaMatrix &lt;- matrix(0, ncol = 10, nrow = 10)<br>thetaMatrix[, 1L] &lt;- 1<br>thetaMatrix[, 10L] &lt;- 100<br>thetaStartIndex &lt;- 1L<br>thetaEndIndex &lt;- 10L<br>alphas &lt;- seq(0, 0.06, length = 16)<br>
alphaCounterStart &lt;- 1L<br>timerem &lt;- rnorm(10, mean = 40, sd = 2)<br><br>r &lt;- fillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)<br>cpp &lt;- cppFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)<br>
cppArma &lt;- cppArmaFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)<br>all.equal(r, cpp, cppArma)<br><br>benchmark(fillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>
        cppFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>        cppArmaFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>
        <br>        columns = c(&quot;test&quot;, &quot;replications&quot;, &quot;elapsed&quot;, &quot;relative&quot;, &quot;user.self&quot;, &quot;sys.self&quot;),<br>        order = &quot;relative&quot;, <br>        replications = n)<br>
<br>Here is some of my output;<br><br>&gt; n &lt;- 1e6<br>&gt; r &lt;- fillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)<br>&gt; cpp &lt;- cppFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)<br>
&gt; cppArma &lt;- cppArmaFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)<br>&gt; all.equal(r, cpp, cppArma)<br>[1] TRUE<br><br>&gt; benchmark(fillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>
+         cppFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>+         cppArmaFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>
+        <br>+         columns = c(&quot;test&quot;, &quot;replications&quot;, &quot;elapsed&quot;, &quot;relative&quot;, &quot;user.self&quot;, &quot;sys.self&quot;),<br>+         order = &quot;relative&quot;,<br>+         replications = n)<br>
&gt; benchmark(fillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>+         cppFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>
+         cppArmaFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem),<br>+        <br>+         columns = c(&quot;test&quot;, &quot;replications&quot;, &quot;elapsed&quot;, &quot;relative&quot;, &quot;user.self&quot;, &quot;sys.self&quot;),<br>
+         order = &quot;relative&quot;,<br>+         replications = n)<br><br>                                                                                                     test replications elapsed relative user.self sys.self<br>
2     cppFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)      1000000   12.22 1.000000     12.17     0.01<br>3 cppArmaFillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)      1000000   15.07 1.233224     15.01     0.02<br>
1        fillMiddleTheta(thetaMatrix, thetaStartIndex, thetaEndIndex, alphas, alphaCounterStart, timerem)      1000000   86.62 7.088380     86.02     0.10<br>