Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.44.0305281240160.14422-100000@gannet.stats>
Date: 2003-05-28T11:43:19Z
From: Brian Ripley
Subject: Slow computation in for loop
In-Reply-To: <MABBLJDICACNFOLGIHJOCEMMDHAA.phgrosjean@sciviews.org>

On Wed, 28 May 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.
> 
> An even better solution is to allocate a data frame or matrix with the final
> size (it seems you can predict it in your example), and then use
> df[n, ] <- result
> where df is your allocated data frame and n is the iteration.

Since we are told the results are all numeric, it would be even better to 
use a pre-computed numeric matrix to store them.  rbind.data.frame is 
about the worst possible choice in such circumstances.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595