<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Hi Konrad. <div><br></div><div>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]. </div><div><br></div><div>Thanks,</div><div><br></div><div>Avi</div><div><br></div><div>[1] <a href="https://www.avrahamadler.com/2018/12/23/the-need-for-speed-part-2-c-vs-fortran-vs-c/">https://www.avrahamadler.com/2018/12/23/the-need-for-speed-part-2-c-vs-fortran-vs-c/</a></div><div><br><div dir="ltr">Sent from my iPhone</div><div dir="ltr"><br><blockquote type="cite">On Aug 10, 2022, at 6:22 PM, Dirk Eddelbuettel <edd@debian.org> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><span></span><br><span>Hi Konrad,</span><br><span></span><br><span>On 10 August 2022 at 08:22, konrad wrote:</span><br><span>| I have a question regarding the cost of .Call. If I implement the </span><br><span>| rosenbrock function in R and in Rcpp. The R version is substentially </span><br><span>| faster then the C++ version. The Rcpp function is basically an R </span><br><span>| function which calls the C++ function using .Call. Which part of the </span><br><span>| code generates this overhead of the Rcpp function. Is it the .Call </span><br><span>| itself or the conversion of the types from R to Rcpp? Or have I done </span><br><span>| something wrong?</span><br><span></span><br><span>It's just a not a meaningful benchmark as there are essentially no operations</span><br><span>on the R side either.</span><br><span></span><br><span>And Rcpp, by making it _convenient_ injects some extra code and tests and</span><br><span>state keeping all of which is documented and for which you have some toggles</span><br><span>to suppress at least parts.</span><br><span></span><br><span>But in short, it's a non-question. By all means keep exploring Rcpp but you</span><br><span>will need something meatier for it to make sense. You should have no problem</span><br><span>finding examples.</span><br><span></span><br><span>Cheers, Dirk</span><br><span></span><br><span>| </span><br><span>| library(Rcpp)</span><br><span>| library(microbenchmark)</span><br><span>| </span><br><span>| </span><br><span>| fr <- function(x) { ## Rosenbrock Banana function</span><br><span>| x1 <- x[1]</span><br><span>| x2 <- x[2]</span><br><span>| 100 * (x2 - x1 * x1)^2 + (1 - x1)^2</span><br><span>| }</span><br><span>| </span><br><span>| sourceCpp(code = "</span><br><span>| #include <Rcpp.h></span><br><span>| </span><br><span>| // [[Rcpp::export]]</span><br><span>| double fr_rcpp(Rcpp::NumericVector x) {</span><br><span>| double x1 = x[0];</span><br><span>| double x2 = x[1];</span><br><span>| return 100 * (x2 - x1 * x1)*(x2 - x1 * x1) + (1 - x1)*(1 - x1);</span><br><span>| }</span><br><span>| ")</span><br><span>| </span><br><span>| x <- c(1, 2)</span><br><span>| identical(fr(x), fr_rcpp(x))</span><br><span>| </span><br><span>| r <- microbenchmark(fr(x), fr_rcpp(x))</span><br><span>| boxplot(r)</span><br><span>| </span><br><span>| </span><br><span>| Thank you very much for your help!</span><br><span>| </span><br><span>| </span><br><span>| All the best,</span><br><span>| </span><br><span>| </span><br><span>| Konrad</span><br><span>| _______________________________________________</span><br><span>| Rcpp-devel mailing list</span><br><span>| Rcpp-devel@lists.r-forge.r-project.org</span><br><span>| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</span><br><span>-- </span><br><span>dirk.eddelbuettel.com | @eddelbuettel | edd@debian.org</span><br><span>_______________________________________________</span><br><span>Rcpp-devel mailing list</span><br><span>Rcpp-devel@lists.r-forge.r-project.org</span><br><span>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</span></div></blockquote></div></body></html>