[Rcpp-devel] use of auxiliary functions

Davor Cubranic cubranic at stat.ubc.ca
Wed Aug 11 23:55:20 CEST 2010


As Romain said, there can be some hidden optimization going on, or the 
difference is not significant compared to the cost of the rest of the 
code. 

But C++ best practice would recommend that you write this particular 
function as you did in dum2, not dum1. It may not matter in this case, 
but it would in others. And it's good to be consistent, which will also 
help someone else reading your code. (But you don't have to put 
parentheses around the return statement's argument.)

Also, making B a constant parameter if it's not supposed to be changed 
inside the function will sooner or later protect you from mistakes. Use 
consts even if you're not comfortable with using references, it's just 
another small bit of help from the compiler.

Davor


On August 11, 2010 12:30:05 pm baptiste auguie wrote:
> 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)));
>   }
> 
> }


More information about the Rcpp-devel mailing list