As I'm a bit new to Rcpp, I might be missing a trick here.
Let's create two matrices:
library(Rcpp)
library(microbenchmark)
P <- matrix(0, 200,500)
for(i in 1:500) P[,i] <- rep(rep(sample(0:1), 2), 25)
Parent_Check <- matrix(0, nrow(P), nrow(P))
I now want the following done:
Test1 <- function(){
for (i in 1:nrow(P)) {
Parent_Check[i,] <- apply(P, 1, function(x) all(x == P[i,]))
}
}
Test1()
I then created a Rcpp version for all() hoping to improve speed, defined as:
Rcpp::cppFunction(
'bool all_C(LogicalVector x) {
// Note the use of is_true to return a bool type.
return is_true(all(x == TRUE));
}
'
)
Checking the speeds using all_C, it proves to be slower:
Test2 <- function(){
for (i in 1:nrow(P)) {
Parent_Check[i,] <- apply(P, 1, function(x) all_C(x == P[i,]))
}
Parent_Check
}
microbenchmark::microbenchmark(Test1(), Test2(), times = 10)
expr min lq mean median uq max neval
Test1() 467.9671 471.1590 488.1784 479.4830 485.4755 578.5338 10
Test2() 544.6561 552.7025 587.8888 570.4416 641.1202 657.7581 10
Trouble is, all_C() is slower than all(), so I suspect the slow speed for Test2() requires a better all_C call as well as a way of avoid apply in the above example.
I tried rewriting apply in Rcpp using this answer, but using this Rcpp apply function makes it even slower.
Any ideas on how to improve the speed of Test1() using Rcpp?