Manos Papadakis
Manos Papadakis

Reputation: 593

Function inside a package is slower than outside the package

I use Rcpp and RcppArmadillo and I have a "strange" problem. Lets say that I have function f1(). I include this function inside my package and run the command "R CMD INSTALL". After it's done I run a benchmark and I realize that f1 is slower about 100 microseconds inside the package than outside. So if a function wants 100ms to finish, in the package it wants about 200+ms.

Code:

functions.cpp

vec f1(vec x){
    vec F( x.size() );
    for( int i = 0; i < x.size(); ++i ){
         // do something 
    }
    return F;
}

exportfunctions.cpp

vec f1(vec x);
RcppExport SEXP MyPackage_f1(SEXP xSEXP) {
BEGIN_RCPP
    RObject  __result;
    RNGScope __rngScope;
    traits::input_parameter< vec >::type      x(xSEXP);
    __result = wrap(f1(x));
   return __result;
END_RCPP
}

exportfunctions.R

f1<- function(x) {
    .Call( ' MyPackage_f1 ' , PACKAGE = ' MyPackage ', x )
}

An example of how my code is been written. I believe the problem is that a function.R calls a function.cpp which calls the final function. But why is this happening inside the package and not in sourceCpp. I can't understand the difference.

Upvotes: 1

Views: 359

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

Briefly:

  • 100ms is a non-issue. You are coming from R which is an interpreted environment

  • Calling a function involves several steps. Finding a function in a package involves some more.

  • See the documentation for .Call() to see how to minimize lookup.

  • See the documentation for NAMESPACE to set identifiers there too.

The latter two points should help close the gap between calling an ad-hoc function in the environment (which is cheaper) verses calling a function from the properly created infrastructure for doing so a.k.a. a package.

Upvotes: 5

Related Questions