[Rcpp-devel] How to increase the coding efficiency
Kasper Daniel Hansen
kasperdanielhansen at gmail.com
Wed Dec 5 04:01:24 CET 2012
On Tue, Dec 4, 2012 at 9:55 PM, Christian Gunning <xian at unm.edu> wrote:
> What exactly do these timings show? A single call you your function?
> How many calls?
> Building on Romain's point: -- a portion of your function's runtime is
> in memory allocation
> (and you have a lot of allocations here).
> If you're calling your function thousands or millions of times, then
> it might pay to closely
> examine your memory allocation strategies and figure out what's
> temporary, for example.
> It looks like you're already using copy_aux_mem = false in a number
> of places, but you're
> allocating a lot of objects -- of approx what size?
> For example, wouldn't this work just as well with one less allocation?
> arma::vec kk = t;
> arma::uvec q1 = arma::find(arma::abs(tp)<h);
> kk.elem(q1) = ((1-arma::pow(tp.elem(q1)/h,2))/h)*0.75;
> // done with q1. let's reuse it.
> q1 = arma::find(arma::abs(tp)>=h);
> // was q2
> You could potentially allocate memory for temporary working space in
> R, grab it with copy_aux_mem = false, write your temp results there,
> and reuse these objects in subsequent function calls. It doesn't make
> sense to go to this trouble, though, if your core algorithm consumes
> the bulk of runtime.
> Have you looked on the armadillo notes r.e. inv? Matrix inversion has
> O(>n^2). You may be aided by pencil-and-paper math here.
Very tangential, but I have always found appendix C of Boyd's convex
optimization to be very useful on numerical linear algebra. Freely
available from the author at
(very nice book, but not something you read in an evening. Appendix C
is something that does not depend on anything else in the book
>> Dear All,
>> I have tried out the first example by using RcppArmadillo, but I am not
>> sure whether the code is efficient or not. And I did the comparison of the
>> computation time.
>> 1) R code using for loop in R: 87.22s
>> 2) R code using apply: 77.86s
>> 3) RcppArmadillo by using for loop in C++: 53.102s
>> 4) RcppArmadillo together with apply in R: 47.310s
>> It is kind of not so big increase. I am wondering whether I used an
>> inefficient way for the C++ coding:
> A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
More information about the Rcpp-devel