[Rcpp-devel] Slower sugar functions through Rcpp than R?

Dirk Eddelbuettel edd at debian.org
Tue Feb 21 12:59:51 CET 2017


On 21 February 2017 at 11:12, Eridk Poliruyt wrote:
| Hi all,
|  
| I am just starting using Rcpp to accelerate some computations. I need to
| evaluate some likelihood using common math functions like beta, lbeta, gamma,
| lgamma, choose, lchoose, etc. But I found that it could be even slower in Rcpp
| than R? Please see the example below using Rcpp..
| 
| // [[Rcpp::export]]
| NumericVector gm(NumericVector& v1)
| {
|   return(gamma(v1));
| }
| 
| In R, I compared the calculation with R as follows,
| > microbenchmark(gm(1:10),gamma(1:10))
| Unit: nanoseconds
|         expr  min   lq    mean median   uq   max neval
|     gm(1:10) 1510 1510 1854.26   1812 1812 13282   100
|  gamma(1:10)  604  605  776.65    906  906  2114   100
| 
| May I ask is this normal and how can I speed up the calculation here?

The approach you took here, "profiling", is useful in finding actual
bottlenecks and hotspots in the code.

But look at gamma():

   R> gamma
   function (x)  .Primitive("gamma")
   R> 

It is already a .Primitive; those are almost always _already compiled_ code.
And that means it is less likely you will score a gain by calling it from
your compiled code.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list