Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active March 25, 2022 00:50
Show Gist options
  • Save louisswarren/2a3da639bc394f5b46e3267a994b8675 to your computer and use it in GitHub Desktop.
Save louisswarren/2a3da639bc394f5b46e3267a994b8675 to your computer and use it in GitHub Desktop.
Demonstration of performance penalty of Rcpp code being wrapped in tryCatch
library(Rcpp)
library(profvis)
cppFunction('
double
call_rnorm(int ignored)
{
Function f("rnorm");
NumericVector x = f(1);
return x[0];
}
')
cppFunction('
double
call_rnorm_fast(int ignored)
{
Function f("rnorm");
Language call(f, 1) ;
NumericVector x = call.fast_eval();
return x[0];
}
')
loop <- function(f) { for (i in c(1:100000)) { f(0) } }
profvis({loop(call_rnorm)}) # ~1800ms -- stack trace includes tryCatch
profvis({loop(call_rnorm_fast)}) # ~400ms -- no tryCatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment