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

Iñaki Ucar iucar at fedoraproject.org
Thu Dec 2 17:48:24 CET 2021


Hi Víthor,

My simulation package makes heavy use of calls to R user functions from a
C++ simulation loop, and therefore greatly benefits from this feature too,
which I think we should promote to default. Meanwhile, take a look at this
Makevars file to see how to activate it:
https://github.com/r-simmer/simmer/blob/master/src/Makevars

Iñaki

El jue., 2 dic. 2021 17:32, Víthor Rosa <vrosafranco at gmail.com> escribió:

> 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 at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20211202/6ce18378/attachment.html>


More information about the Rcpp-devel mailing list