Look at the following simple code and operations:
library(Rcpp)
library(microbenchmark)
set.seed(100)
x <- sample(0:1, 1000000, replace = TRUE)
y <- sample(0:1, 1000000, replace = TRUE)
cppFunction('LogicalVector is_equal_c(NumericVector x, NumericVector y) {
return x == y;
}')
is_equal_R <- function(x, y) {
return(x==y)
}
mbm <- microbenchmark(c = is_equal_c(x,y),
R = is_equal_R(x,y)
)
mbm
it gives the following performance of the execution speed:
Unit: milliseconds
expr min lq mean median uq max neval cld
c 6.4132 6.6896 10.961774 11.2421 12.63245 102.5480 100 b
R 1.2555 1.2994 1.766561 1.3327 1.38220 9.0022 100 a
Simple R equality operator is 8 times faster than the Rcpp. Why is that so and is there a way how to make Rcpp code at least as fast as R simple vector equality operator?