[R] “For” calculation is so slow

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue May 22 16:35:32 CEST 2012


Thanks, I will take that factor of 100 anytime rather than keep some syntactic familiarity from other languages.

Not to say I don't ever use loops, but I always try to vectorize the inner loop, if not the inner two loops.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Petr PIKAL <petr.pikal at precheza.cz> wrote:

>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