The problem with your solution is that the subset allocates another matrix that takes time.
You have two solutions:
If the time spent using sum on the whole matrix is ββgood with you, you can use colSums on the whole matrix and multiply the result:
sum(colSums(m0)[1:900])
Or you can use Rcpp to calculate sum with a subset without copying the matrix.
#include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double sumSub(const NumericMatrix& x, const IntegerVector& colInd) { double sum = 0; for (IntegerVector::const_iterator it = colInd.begin(); it != colInd.end(); ++it) { int j = *it - 1; for (int i = 0; i < x.nrow(); i++) { sum += x(i, j); } } return sum; } microbenchmark(m0[, 1:900], sum(m0[, 1:900]), sum(r0[,1:900]), sum(m0), sum(colSums(m0)[1:900]), sumSub(m0, 1:900)) Unit: milliseconds expr min lq mean median uq max neval m0[, 1:900] 4.831616 5.447749 5.641096 5.675774 5.861052 6.418266 100 sum(m0[, 1:900]) 6.103985 6.475921 7.052001 6.723035 6.999226 37.085345 100 sum(r0[, 1:900]) 6.224850 6.449210 6.728681 6.705366 6.943689 7.565842 100 sum(m0) 1.110073 1.145906 1.175224 1.168696 1.197889 1.269589 100 sum(colSums(m0)[1:900]) 1.113834 1.141411 1.178913 1.168312 1.201827 1.408785 100 sumSub(m0, 1:900) 1.337188 1.368383 1.404744 1.390846 1.415434 2.459361 100
You can use the deploy optimization to further optimize the version of Rcpp.
source share