[Rcpp-devel] cost of .Call

Dirk Eddelbuettel edd at debian.org
Wed Aug 10 17:22:03 CEST 2022


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
-- 
dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list