<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Dear all,</p>
<p><br>
</p>
<p>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?</p>
<p><font size="1"><br>
</font></p>
<p><font size="1">library(Rcpp)<br>
library(microbenchmark)<br>
<br>
<br>
fr <- function(x) { ## Rosenbrock Banana function<br>
x1 <- x[1]<br>
x2 <- x[2]<br>
100 * (x2 - x1 * x1)^2 + (1 - x1)^2<br>
}<br>
<br>
sourceCpp(code = "<br>
#include <Rcpp.h><br>
<br>
// [[Rcpp::export]]<br>
double fr_rcpp(Rcpp::NumericVector x) {<br>
double x1 = x[0];<br>
double x2 = x[1];<br>
return 100 * (x2 - x1 * x1)*(x2 - x1 * x1) + (1 - x1)*(1 -
x1);<br>
}<br>
")<br>
<br>
x <- c(1, 2)<br>
identical(fr(x), fr_rcpp(x))</font></p>
<p><font size="1">r <- microbenchmark(fr(x), fr_rcpp(x))<br>
boxplot(r)</font></p>
<p><br>
</p>
<p>Thank you very much for your help!</p>
<p><br>
</p>
<p>All the best,</p>
<p><br>
</p>
<p>Konrad<br>
</p>
</body>
</html>