Why is zoo::rollmean slow compared to a simple Rcpp implementation?
问题 zoo::rollmean is a helpful function that returns the rolling mean of a time series; for vector x of length n and window size k it returns the vector c(mean(x[1:k]), mean(x[2:(k+1)]), ..., mean(x[(n-k+1):n])) . I noticed that it seemed to be running slowly for some code I was developing, so I wrote my own version using the Rcpp package and a simple for loop: library(Rcpp) cppFunction("NumericVector rmRcpp(NumericVector dat, const int window) { const int n = dat.size(); NumericVector ret(n