[R] Performance note: Preallocating helps? and two questions

Emmanuel Paradis paradis at isem.univ-montp2.fr
Wed Nov 1 17:40:55 CET 2000


At 06:00 01/11/00 -0800, you wrote:
>hi-
> in r 1.1 on windows 2000
> with length(AU) of 35833
> AUcap30<-0
> for(i in 1:length(AU))AUcap30[i]<-min(30,AU[i]) 
>took over an hour on pentium II 300 mhertz (I esc'ed before it finished)
>but
>AUcap30<-AU 
> for(i in 1:length(AU))AUcap30[i]<-min(30,AU[i]) 
>is very quick (a few seconds)
>
>Is this performance difference common in r (ie is linux the same way)?

I do not think this is a problem with memory. In the first case, you have
length(AUcap30)==1, thus your loop is non-sense. I would have expected an
error message here like "subscript out of range for AUcap30" (is this a
bug? it seems R is trapped in the loop). In the second case, you have:

> length(AU)==length(AUcap30)
[1] TRUE

and things are fine.

Emmanuel Paradis


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list