Skip to content
Back to formatted view

Raw Message

Message-ID: <22700.11191.353157.576861@max.eddelbuettel.com>
Date: 2017-02-21T11:59:51Z
From: Dirk Eddelbuettel
Subject: [Rcpp-devel] Slower sugar functions through Rcpp than R?
In-Reply-To: <CAGkGB64q+HxiU3pNT4_RGjYFsoX=KuEXv5D2q8RhoXBB_TzCFg@mail.gmail.com>

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