title: My C++ functions with Rcpp::List inputs are very slow
body: <p>While C++ and specifically the Rcpp package have been tremendously helpful to me in speeding up my codes, I noticed that my C++ functions which have a list or data frame input argument(arguments of the form Rcpp::DataFrame and Rcpp::List) are very slower compared to my other C++ functions. I wrote a sample code and I wanted to ask for tricks that can make my code faster:</p>
  
  <p>First, let's simulate a List in R that contains two Lists inside of it. Consider <strong>myList</strong> as a list that includes two lists - measure1 and measure2. measure1 and measure2 are lists themselves each include vectors of measurements for  subjects. Here is the R code:</p>
  
  <pre><code>lappend &lt;- function(lst, ...){
    lst &lt;- c(lst, list(...))
  return(lst)
  }
  
  nSub &lt;- 30
  meas1 &lt;- list()
  meas2 &lt;- list()
  for (i in 1:nSub){
    meas1 &lt;- lappend(meas1, rnorm(10))
    meas2 &lt;- lappend(meas2, rnorm(10))
  }
  myList &lt;- list(meas1 = meas1, meas2 = meas2)
  </code></pre>
  
  <p>Now, suppose I want a C++ function that for each subject, finds summation of measure1 and summation of measure 2 and then creates two new measurements based on these two summation. Finally the function should return these new measurements as a list. </p>
  
  <pre><code>// [[Rcpp::depends(RcppArmadillo)]]
  #include &lt;RcppArmadillo.h&gt;
  #include &lt;Rcpp.h&gt;
  
  // [[Rcpp::export]]
  Rcpp::List mySlowListFn(Rcpp::List myList, int nSub){
     arma::vec myMult(nSub);
     arma::vec myDiv(nSub);
     for (int i = 0; i &lt; nSub; i++){
       arma::vec meas1_i = Rcpp::as&lt;arma::vec&gt;(Rcpp::as&lt;Rcpp::List&gt;(myList["meas1"])[i]);
       arma::vec meas2_i = Rcpp::as&lt;arma::vec&gt;(Rcpp::as&lt;Rcpp::List&gt;(myList["meas2"])[i]);
       myMult[i] = arma::sum(meas1_i)*arma::sum(meas2_i);
       myDiv[i] = arma::sum(meas1_i)/arma::sum(meas2_i);
     }
     return Rcpp::List::create(Rcpp::Named("myMult") = myMult, 
                               Rcpp::Named("myDiv") = myDiv);
  }
  </code></pre>
  
  <p>How can I make the function above faster? I'm particularly looking for ideas that keep the input and output lists in the code (since in my own program dealing with lists is inevitable), but with some tricks to reduce some overhead time. One thing that I thought of was:</p>
  
  <pre><code> Rcpp::List mySlowListFn(const Rcpp::List&amp; myList, int nSub)
  </code></pre>
  
  <p>Thanks very much for your help.</p>