Closed hdarjus closed 5 years ago
library(Rcpp)
library(microbenchmark)
cppFunction("
NumericVector foo1(const NumericVector& x) {
return x;
}",
depends="RcppArmadillo",
plugins="cpp11")
cppFunction("
NumericVector foo2(const arma::vec& x) {
return wrap(x);
}",
depends="RcppArmadillo",
plugins="cpp11")
cppFunction("
arma::vec foo3(const arma::vec& x) {
return x;
}",
depends="RcppArmadillo",
plugins="cpp11")
cppFunction("
arma::vec foo4(const NumericVector& x) {
return x;
}",
depends="RcppArmadillo",
plugins="cpp11")
ii <- rep_len(0, 1000000)
microbenchmark(foo1(ii), foo2(ii), foo3(ii), foo4(ii))
returns
# Unit: microseconds
# expr min lq mean median uq max neval
# foo1(ii) 1.304 2.2005 5.33173 5.779 7.1365 16.304 100
# foo2(ii) 1357.309 2984.1665 3378.07325 3090.318 3206.5895 6242.189 100
# foo3(ii) 4114.598 6663.7965 6544.44861 6764.386 6864.1900 8754.701 100
# foo4(ii) 4306.641 5848.8875 6024.94347 6121.225 6277.5035 7692.962 100
This turns out not to be a problem for some reason. I tried to improve the runtime just by replacing the classes, nothing happened. Also, check the file below from RcppSMC (Eddelbuettel and others), he must be using it the right way. https://github.com/rcppsmc/rcppsmc/blob/782bf651978d2c9d6d6df1d960c2a494c1bf8eeb/src/RcppExports.cpp#L11
take and return Rcpp objects from and to R instead of arma objects. arma objects get copied by default