[Rcpp-devel] use of auxiliary functions

baptiste auguie baptiste.auguie at googlemail.com
Wed Aug 11 21:30:05 CEST 2010


I think I understand the principle, however with my best effort I
cannot find a test case. Here I thought I'd be passing an arbitrarily
large arma matrix to some function, but again the timing is not
convincing (in fact, I had to stop the execution once with the dummy2
version),

using namespace Rcpp ;
using namespace RcppArmadillo ;

extern "C" {

  arma::mat dum1(arma::mat B)
  {
    return (B*38);
  }

  arma::mat dum2(const arma::mat& B)
  {
    return (B*38);
  }

  RCPP_FUNCTION_2(NumericMatrix, dummy1, NumericMatrix A, int N) {
    arma::mat B = Rcpp::as< arma::mat >( A ) ;
    return wrap(dum1(repmat(B, N, N)));
  }

  RCPP_FUNCTION_2(NumericMatrix, dummy2, NumericMatrix A, int N) {
    arma::mat B = Rcpp::as< arma::mat >( A ) ;
    return wrap(dum2(repmat(B, N, N)));
  }

}

}

Regards,

baptiste


On 11 August 2010 19:21, Davor Cubranic <cubranic at stat.ubc.ca> wrote:
> On August 11, 2010 02:48:23 am romain at r-enthusiasts.com wrote:
>> Le 10 août 2010 à 22:10, baptiste auguie
> <baptiste.auguie at googlemail.com> a écrit :
>> > OK, thanks. I have not been able to produce a minimal code that
>> > would exhibit an improved performance using this
>> > passing-by-reference idea.
>>
>> Hi,
>>
>> It would usually make a difference when copying the object that is
>> passed by value is "expensive" to copy. Rcpp objects are very cheap
>> to copy and both copies refer to the same actual data (the same
>> SEXP).
>>
>> For internal code my recommendation would be to always use pass by
>> reference and do an explicit call to clone when a real copy
>> (different SEXP) is needed.
>
> In other words, if you were passing large arma::mat's, then const
> references should make a real difference.
>
> Davor
>


More information about the Rcpp-devel mailing list