Rcpp 导致性能提升(特别是在列数较多的情况下)。
library(Rcpp)
library(inline)
src <- '
Rcpp::NumericMatrix dataR(data);
Rcpp::NumericVector weightsR(weights);
int ncol = dataR.ncol();
Rcpp::NumericVector sumR(ncol);
for (int col = 0; col<ncol; col++){
sumR[col] = Rcpp::sum(dataR( _, col)*weightsR);
}
return Rcpp::wrap(sumR);'
weighted.colSums <- cxxfunction(
signature(data="numeric", weights="numeric"), src, plugin="Rcpp")
data <- matrix(as.numeric(1:1e7),1e5,100) # warning large object
weights <- 1:1e5/1e5
all.equal(colSums(data*weights), weighted.colSums(data, weights))
## [1] TRUE
print(system.time(colSums(data*weights)))
## user system elapsed
## 0.065 0.001 0.064
print(system.time(as.vector(weighted.colSums(data, weights))))
## user system elapsed
## 0.019 0.001 0.019
all.equal(as.vector(weights %*% data), weighted.colSums(data, weights))
## [1] TRUE
print(system.time(weights %*% data))
## user system elapsed
## 0.066 0.001 0.066