Skip to content

[Rcpp-devel] cost of .Call

3 messages · Konrad, Dirk Eddelbuettel, Avraham Adler

#
Dear all,


I have a question regarding the cost of .Call. If I implement the 
rosenbrock function in R and in Rcpp. The R version is substentially 
faster then the C++ version. The Rcpp function is basically an R 
function which calls the C++ function using .Call. Which part of the 
code generates this overhead of the Rcpp function. Is it the .Call 
itself or the conversion of the types from R to Rcpp? Or have I done 
something wrong?


library(Rcpp)
library(microbenchmark)


fr <- function(x) {   ## Rosenbrock Banana function
   x1 <- x[1]
   x2 <- x[2]
   100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}

sourceCpp(code = "
#include <Rcpp.h>

// [[Rcpp::export]]
double fr_rcpp(Rcpp::NumericVector x) {
   double x1 = x[0];
   double x2 = x[1];
   return 100 * (x2 - x1 * x1)*(x2 - x1 * x1) + (1 - x1)*(1 - x1);
}
")

x <- c(1, 2)
identical(fr(x), fr_rcpp(x))

r <- microbenchmark(fr(x), fr_rcpp(x))
boxplot(r)


Thank you very much for your help!


All the best,


Konrad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20220810/abe5e12f/attachment.html>
#
Hi Konrad,
On 10 August 2022 at 08:22, konrad wrote:
| I have a question regarding the cost of .Call. If I implement the 
| rosenbrock function in R and in Rcpp. The R version is substentially 
| faster then the C++ version. The Rcpp function is basically an R 
| function which calls the C++ function using .Call. Which part of the 
| code generates this overhead of the Rcpp function. Is it the .Call 
| itself or the conversion of the types from R to Rcpp? Or have I done 
| something wrong?

It's just a not a meaningful benchmark as there are essentially no operations
on the R side either.

And Rcpp, by making it _convenient_ injects some extra code and tests and
state keeping all of which is documented and for which you have some toggles
to suppress at least parts.

But in short, it's a non-question. By all means keep exploring Rcpp but you
will need something meatier for it to make sense. You should have no problem
finding examples.

Cheers, Dirk

| 
| library(Rcpp)
| library(microbenchmark)
| 
| 
| fr <- function(x) {   ## Rosenbrock Banana function
|    x1 <- x[1]
|    x2 <- x[2]
|    100 * (x2 - x1 * x1)^2 + (1 - x1)^2
| }
| 
| sourceCpp(code = "
| #include <Rcpp.h>
| 
| // [[Rcpp::export]]
| double fr_rcpp(Rcpp::NumericVector x) {
|    double x1 = x[0];
|    double x2 = x[1];
|    return 100 * (x2 - x1 * x1)*(x2 - x1 * x1) + (1 - x1)*(1 - x1);
| }
| ")
| 
| x <- c(1, 2)
| identical(fr(x), fr_rcpp(x))
| 
| r <- microbenchmark(fr(x), fr_rcpp(x))
| boxplot(r)
| 
| 
| Thank you very much for your help!
| 
| 
| All the best,
| 
| 
| Konrad
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Hi Konrad. 

As Dirk said, Rcpp makes life easy for the programmer by taking care of some of the background work automatically. So there may be a time vs. speed trade off against bespoke handwritten code. I have a comparison between a few implementations (base, C,Rcpp, Fortran) of a relatively simple function which may interest you [1]. 

Thanks,

Avi

[1] https://www.avrahamadler.com/2018/12/23/the-need-for-speed-part-2-c-vs-fortran-vs-c/

Sent from my iPhone
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20220810/31ee94e1/attachment.html>