Consider the following functions foo
and bar
(differs only in printing warning) and their R equivalents fooR
and barR
:
I'm not sure if you realize from your benchmark that Rcpp
is:
Now, when you are throwing a warning, in both Rcpp
and base R
, the following is occurring:
STDERR
is opened. (Hence, red text instead of STDOUT
's black)STDERR
In Rcpp
, the low-level reference helps lower the total amount of time it takes to repeat the above procedure and return control to the loop vs. R's handler that sits higher up on the stack.
Again, the time it takes to complete this with Rcpp is normal as it has to cede control of the process back to R, print the message, then return to the loop. The best way to think about the loss of speed from the pure loop case is you decided to call an R based function in a C++ loop instead of an R loop expecting a speedup.
To be honest, I'm a bit amazed that Rcpp
was able to be 2x faster than the R equivalent.