[R] Benefit of Iterators (package iterator)

Doran, Harold HDoran at air.org
Thu Dec 8 15:20:03 CET 2016


R-Help (and package author)

I'm trying to understand within the context of R what the benefit of using an iterator is. My only goal in using the foreach package is to improve computational speed with some embarrassingly parallel tasks I have to compute.

I took the example found at the link below to provide a reproducible example and ran it in a "conventional" way to iterate in a loop and the timing suggests here (as well as with my actual project) that using an iterator generates the same object, but at a much slower speed.

If I can get the same thing faster without using an iterator what would be the potential of its use?	

https://msdn.microsoft.com/en-us/microsoft-r/foreach

> library(doParallel)
> cl <- makeCluster(8) 
> registerDoParallel(cl)
> x <- matrix(rnorm(1000000), ncol=1000)
> itx <- iter(x, by='row')
> system.time(r1 <- foreach(i=itx, .combine=c) %dopar% mean(i))
   user  system elapsed 
   0.40    0.08    0.87 
> system.time(r2 <- foreach(i= 1:nrow(x), .combine=c) %dopar% mean(x[i,]))
   user  system elapsed 
   0.41    0.03    0.81 
> all.equal(r1,r2)
[1] TRUE



More information about the R-help mailing list