I have problem by using foreach
package in R. In fact, when I compile this code :
tmp=proc.time()
x<-for(i in 1:1000){sqrt(i)}
x
proc.time()-tmp
and this code :
tmp=proc.time()
x<- foreach(i=1:1000) %dopar% sqrt(i)
x
proc.time()-tmp
The R console posts for Parallel Computing :
utilisateur système écoulé
0.464 0.776 0.705
and for the normal loop :
utilisateur système écoulé
0.001 0.000 0.001
So the normal loop runs faster... Is it normal?
Thanks for your help.
Parallel processing won't speed up simple operations like
sqrt(x)
. Ideally you use it for more complex operations, or you do something like,It takes more time to switch processes than it does to those tasks. If you look at the processors used in your system monitor/task manager, you'll see that only one processor is used, regardless of the backend you set up.
Edit: It seems that you have no parallel backend set up for your foreach loop, so it will default to sequential mode anyway. An easy way to set up the parallel backend is
Depending on your system, you may need to do more outside of R in order to get the backend set up.