[Rcpp-devel] Rcpp::plugins - Unwinding protection

VĂ­thor Rosa vrosafranco at gmail.com
Thu Dec 2 17:31:41 CET 2021


Hi everyone,

I am using Rcpp to develop my own package and one of my functions calls
back into R for a given amount of iterations. To speed up the process, I
wanted to use Rcpp::unwindProtect. However, I couldn't find
on Rcpp's documentation what is the correct way to use this plugin.
Therefore, I would like to ask you for some guidance, if possible.

Reproducible toy example:

````````````````
require(Rcpp)
Rcpp::cppFunction("
  NumericVector test_f(NumericMatrix x, Function f) {
    NumericVector out(x.ncol());

    for (int iter = 0; iter < x.ncol(); iter++) {
      out[iter] = as<double>(f(x(_ , iter)));
    }

    return out;
  }
")

x <- matrix(1.2:9.2, 3, 3)

colSums(x)
f <- function(x) sum(x)
test_f(x, f)

require(rbenchmark)
rbenchmark::benchmark("R1"=colSums(x),
                                        "R2"=test_f(x, f),
                                        replications=10000)
````````````````

Best,
Vithor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20211202/7dfd8e81/attachment.html>


More information about the Rcpp-devel mailing list