Yes, the main issue for my coding is the allocation of memory.  And I have fixed one of the biggest memory allocation issue: 4000 by 4000 diagonal matrix. And since I am not familiar with Rcpp and RcppArmadillo, I have no idea how to reuse the memory. I hope I can have some materials to learn this. Thanks.<br clear="all">
<div><br></div><div> </div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">What exactly do these timings show?  A single call you your function?<br>

How many calls?<br>
<br></blockquote><div>Here I called my function for 100 times.<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Building on Romain's point: -- a portion of your function's runtime is<br>
in memory allocation<br>
(and you have a lot of allocations here).<br>
If you're calling your function thousands or millions of times, then<br>
it might pay to closely<br>
examine your memory allocation strategies and figure out what's<br>
temporary, for example.<br>
It looks like you're already using  copy_aux_mem = false in a number<br>
of places, but you're<br>
allocating a lot of objects -- of approx what size?<br>
<br>
For example, wouldn't this work just as well with one less allocation?<br>
<div class="im">arma::vec kk = t;<br>
</div><div class="im">arma::uvec q1 = arma::find(arma::abs(tp)<h);<br>
</div>kk.elem(q1) = ((1-arma::pow(tp.elem(q1)/h,2))/h)*0.75;<br>
// done with q1.  let's reuse it.<br>
q1 = arma::find(arma::abs(tp)>=h);<br>
// was q2<br>
kk.elem(q1).zeros();<br>
<br>
You could potentially allocate memory for temporary working space in<br>
R, grab it with copy_aux_mem = false, write your temp results there,<br>
and reuse these objects in subsequent function calls.  It doesn't make<br>
sense to go to this trouble, though, if your core algorithm consumes<br>
the bulk of runtime.<br>
<br>
Have you looked on the armadillo notes r.e. inv?  Matrix inversion has<br>
O(>n^2).  You may be aided by pencil-and-paper math here.<br>
<a href="http://arma.sourceforge.net/docs.html#inv" target="_blank">http://arma.sourceforge.net/docs.html#inv</a><br>
<br></blockquote><div>Here my matrix for inverse is only 4 by 4, so I think it's ok.<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
best,<br>
Christian<br>
<div class="HOEnZb"><div class="h5"><br>
> Dear All,<br>
> I have tried out the first example by using RcppArmadillo, but I am not<br>
> sure whether the code is efficient or not. And I did the comparison of the<br>
> computation time.<br>
><br>
> 1) R code using for loop in R: 87.22s<br>
> 2) R code using apply: 77.86s<br>
> 3) RcppArmadillo by using for loop in C++: 53.102s<br>
> 4) RcppArmadillo together with apply in R: 47.310s<br>
><br>
> It is kind of not so big increase. I am wondering whether I used an<br>
> inefficient way for the C++ coding:<br>
<br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!<br>
</font></span></blockquote></div><br>