R - loops in parallel with doSMP and foreach

I just discovered a doSMP package that makes it possible to execute foreach in parallel.

Can someone point out what I'm doing wrong with the following simple example? Indeed, using doSMP and foreach in this way significantly increases the computational time ...

Thank!

#------The "naive" method------
mat <- matrix(NA, nrow=1000, ncol=10000)
ptime <- system.time({
for(i in 1:10000)
{
    mat[, i] <- rnorm(1000)
}
})[3]

> ptime
elapsed 
   1.14 
#------------------------------

#------Using doSMP and foreach------
library(doSMP)
w <- startWorkers(2)
registerDoSMP(w)

ptime <- system.time({
mat2 <- foreach(i=1:10000, .combine="cbind") %dopar% rnorm(1000)
})[3]

stopWorkers(w)

> ptime
elapsed 
  10.26 
#-----------------------------------

NB I posted a very similar question here , where I got an alternative ... but still, I would like to understand why this does not work, and ask experts .stackexchange statistics for an opinion

change

What do you think, the answer may be "Performing many tiny tasks in parallel, as a rule, takes longer than doing them sequentially" ???

0

All Articles