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
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to