[R] Slow computation in for loop

Ross Ihaka ihaka at stat.auckland.ac.nz
Wed May 28 22:57:05 CEST 2003


Philippe Grosjean wrote:
> I suspect that your problem comes from the rbind(). I have also noticed an
> exponentially slower execution with the increase of the size of the data
> frame that you rbind()s. It is much faster to rbind() several separated
> temporary data frames (let's say, ten by ten loops), and then to rbind()
> them all together.

Got it in one.  This is one place where Splus performance is much better 
than R.  Some simulations I did makes it look like Splus does not just 
enlarge objects one element at a time. Instead, the underlying memory is 
enlarged in larger increments and additions to the array use this hidden 
space.

I thought about adding this feature, but in the end decided that 
pre-allocating the result is a better solution (the Splus enlargement 
strategy doesn't work when elements are prepended).  At one one point 
there was a slot in the object header which could have served to hold 
the "true length" of objects, but I think that Luke Tierney has used it 
for other puposes.

-- 
Ross Ihaka                         Email:  ihaka at stat.auckland.ac.nz
Department of Statistics           Phone:  (64-9) 373-7599 x 85054
University of Auckland             Fax:    (64-9) 373-7018
Private Bag 92019, Auckland
New Zealand




More information about the R-help mailing list