[R] Slow computation in for loop

Philippe Grosjean phgrosjean at sciviews.org
Fri May 30 10:12: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.

Ross Ihaka answered:
>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.

Of course, we could dream of a faster rbind() in such circumstances (Prof.
Brian Ripley pointed also that using a data frame is much less efficient
than a matrix if all entries are numbers). However, if nobody has objections
on the programming style using a preallocated matrix, perhaps should it be
useful to add this tip somewhere in the documentation (FAQ, ...?):

# Solution using rbind()
all.results <- NULL
system.time(
for (i in 1:10000) {
	result <- rnorm(5)
	all.results <- rbind(all.results, result)
})

# Solution using a preallocated matrix
all.results <- matrix(nrow=10000, ncol=5)
system.time(
for (i in 1:10000) {
	result <- rnorm(5)
	all.results[i, ] <- result
})

On my P IV 1.6 Ghz, it took 11.74 sec using rbind() and 0.36 sec using the
preallocated matrix.
Best,

Philippe Grosjean

...........]<(({?<...............<?}))><...............................
 ) ) ) ) )
( ( ( ( (       Dr. Philippe Grosjean
 ) ) ) ) )
( ( ( ( (       LOV, UMR 7093
 ) ) ) ) )      Station Zoologique
( ( ( ( (       Observatoire Oceanologique
 ) ) ) ) )      BP 28
( ( ( ( (       06234 Villefranche sur mer cedex
 ) ) ) ) )      France
( ( ( ( (
 ) ) ) ) )      tel: +33.4.93.76.38.18, fax: +33.4.93.76.38.34
( ( ( ( (
 ) ) ) ) )      e-mail: phgrosjean at sciviews.org
( ( ( ( (       SciViews project coordinator (http://www.sciviews.org)
 ) ) ) ) )
.......................................................................




More information about the R-help mailing list