[R] “For” calculation is so slow

Petr PIKAL petr.pikal at precheza.cz
Tue May 22 16:07:04 CEST 2012


And as followup

> system.time(d<-10000000*10000001/2)
   user  system elapsed 
   0.02    0.00    0.02 
> identical(a,b,d)
[1] TRUE
>

Regards
Petr

> 
> Hi
> 
> > 
> > For loops are really, really slow in R. In general, you want to avoid 
> them
> 
> I strongly disagree. ***Proper*** use of looping is quite convenient and 

> reasonably fast.
> 
> Consider
> 
> > system.time( {
> + a=0
> + for (i in 1:10000000) {
> + a <-a+i
> + }
> + a
> + })
>    user  system elapsed 
>   10.22    0.02   10.28 
> > 
> > system.time(b<-sum(as.numeric(1:10000000)))
>    user  system elapsed 
>    0.09    0.01    0.11 
> > identical(a,b)
> [1] TRUE
> 
> It is usually implementing C habits into R code what makes looping slow. 

> The slowest part of a program is usually programming and it is 
influenced 
> mainly by programmer.
> 
> Regards
> Petr
> 
> > like the plague. If you absolutely must insist on using them in large,
> > computationally intense and complex code, consider implementing the 
> relevant
> > parts in C, say, and calling that from R.
> > 
> > Staying within R, you can probably considerably speed up that code by
> > storing gx and gy as a multi-dimensional arrays. (e.g. for sample 
data,
> > something like
> > 
> > rawGy = sample( 1:240, 240^2* 241, replace = T)
> > rawGx = sample( 1:240, 240^2 *241, replace = T)
> > gx = array(rawGx, dim = c(length(s) - 1, 240,  max(rawGx)+1  ) )
> > gy = array(rawGy, dim = c(length(s) - 1, 240,  max(rawGy)+1 ) )
> > 
> >  ), in which case, you can easily do the computation without loops by
> > 
> > gxa = (gx[ ,a,1]+ 1)
> > gya =(gy[ ,a, 1] +1)
> > uv = gx[cbind(1:(length(s) - 1) , b, gxa)] / gx[cbind(1:(length(s) - 
1) 
> , a,
> > gxa)]  - gy[cbind(1:(length(s) - 1) ,b, gya)]/gy[cbind(1:(length(s) - 
1) 
> ,a,
> > gya)]
> > 
> > or similar, which will be enormously faster (on my computer, there's 
an 
> over
> > 30x speed up). With a bit of thought, I'm sure you can also figure out 

> how
> > to let it vectorise in a, as well...
> > 
> > Zhou
> > 
> > --
> > View this message in context: http://r.789695.n4.nabble.com/For-
> > calculation-is-so-slow-tp4630830p4630855.html
> > Sent from the R help mailing list archive at Nabble.com.
> > 
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list