Last active
March 25, 2022 00:50
-
-
Save louisswarren/2a3da639bc394f5b46e3267a994b8675 to your computer and use it in GitHub Desktop.
Demonstration of performance penalty of Rcpp code being wrapped in tryCatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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