I'm doing a rolling regression analysis and wanted to improve execution speed using multiple cores of my PC. The code i tried looks like this:
library(doParallel)
y <- reg.data[,1][1:15000]
x1 <- reg.data[,2][1:15000]
x2 <- reg.data[,3][1:15000]
windowSize = 8820
registerDoParallel(cores=6)
ptm <- proc.time()
z1 <- foreach(i=seq(1, (length(y)-windowSize+1), 1), .combine=rbind) %do% {
idx <- i:(i+windowSize-1)
coefficients(lm(y[idx]~0+x1[idx]+x2[idx]))
}
print(proc.time() - ptm)
The code executes and produces correct results. The problem is that no matter what i am doing i get roughly the the same result in terms of time spent, if it is 2 cores or 6 i've registered with registerDoParallel(). Which is in sharp contrast to what many users have reported on similar tasks. Looking at TaskManager in Windows i can see the that all 6 cores are doing something and there is virtually no speedup compared to 2 cores (less that a second on a total ~ 60 seconds execution time). Any ideas what i am doing wrong?