Hi I am doing an simulation, and I a large proportion of the simulation time is taken up by memory allocations. I am creating an object, and storing it in a list of those objects. essentially: x <- list() for (t in 1:500) { x[1] <- new("track") } I would like to initialize in one go, to avoid the continuous reallocation of memory when a new "track" is added, and fill it wit the object created by new("track"). How can I do this? thanks Rainer -- Rainer M. Krug, Centre of Excellence for Invasion Biology, Stellenbosch University, South Africa
Hi, Here are a few ways: rep( list( new("track") ), 5 ) lapply( 1:5, function(x) new("track") ) list( new("track") ) [ rep(1, 5 ) ] Romain Rainer M Krug wrote:> Hi > > I am doing an simulation, and I a large proportion of the simulation > time is taken up by memory allocations. > > I am creating an object, and storing it in a list of those objects. > > essentially: > > x <- list() > for (t in 1:500) { > x[1] <- new("track") > } > > I would like to initialize in one go, to avoid the continuous > reallocation of memory when a new "track" is added, and fill it wit > the object created by new("track"). > > How can I do this? > > thanks > > Rainer >-- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
On 6/1/2009 6:08 AM, Rainer M Krug wrote:> Hi > > I am doing an simulation, and I a large proportion of the simulation > time is taken up by memory allocations. > > I am creating an object, and storing it in a list of those objects. > > essentially: > > x <- list() > for (t in 1:500) { > x[1] <- new("track") > } > > I would like to initialize in one go, to avoid the continuous > reallocation of memory when a new "track" is added, and fill it wit > the object created by new("track"). > > How can I do this?Does this help? x <- vector("list", 500) for(i in 1:500){x[[i]] <- runif(30)}> thanks > > Rainer-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
On Mon, Jun 1, 2009 at 12:19 PM, Romain Francois <romain.francois at dbmail.com> wrote:> Hi, > > Here are a few ways:Thanks - I like it when I have a choice.> > rep( list( new("track") ), 5 )OK - This one is executing new("track") once, and copying it into a list.> lapply( 1:5, function(x) new("track") )This one executes new("track") five times and creates the list. no <- new("track") lapply( 1:5, function(x) return(no)) should do the same, only faster, as new() is executed only once.> list( new("track") ) [ rep(1, 5 ) ]now this is a really sneaky one - I like it, although it is the most difficult to understand. Thanks for these options. I think I will go with the first one, as it is the most intuitive one. Thanks, Rainer> > Romain > > Rainer M Krug wrote: >> >> Hi >> >> I am doing an simulation, and I a large proportion of the simulation >> time is taken up by memory allocations. >> >> I am creating an object, and storing it in a list of those objects. >> >> essentially: >> >> x <- list() >> for (t in 1:500) { >> ?x[1] <- new("track") >> } >> >> I would like to initialize in one go, to avoid the continuous >> reallocation of memory when a new "track" is added, and fill it wit >> the object created by new("track"). >> >> How can I do this? >> >> thanks >> >> Rainer >> > > > -- > Romain Francois > Independent R Consultant > +33(0) 6 28 91 30 30 > http://romainfrancois.blog.free.fr > > >-- Rainer M. Krug, Centre of Excellence for Invasion Biology, Stellenbosch University, South Africa
On Mon, Jun 1, 2009 at 12:27 PM, Chuck Cleland <ccleland at optonline.net> wrote:> On 6/1/2009 6:08 AM, Rainer M Krug wrote: >> Hi >> >> I am doing an simulation, and I a large proportion of the simulation >> time is taken up by memory allocations. >> >> I am creating an object, and storing it in a list of those objects. >> >> essentially: >> >> x <- list() >> for (t in 1:500) { >> ?x[1] <- new("track") >> }And another solution. Thanks. I have actually never looked at the vector() function - seems to be quite useful. Thanks and Cheers, Rainer>> >> I would like to initialize in one go, to avoid the continuous >> reallocation of memory when a new "track" is added, and fill it wit >> the object created by new("track"). >> >> How can I do this? > > ?Does this help? > > x <- vector("list", 500) > for(i in 1:500){x[[i]] <- runif(30)} > >> thanks >> >> Rainer > > -- > Chuck Cleland, Ph.D. > NDRI, Inc. (www.ndri.org) > 71 West 23rd Street, 8th floor > New York, NY 10010 > tel: (212) 845-4495 (Tu, Th) > tel: (732) 512-0171 (M, W, F) > fax: (917) 438-0894 >-- Rainer M. Krug, Centre of Excellence for Invasion Biology, Stellenbosch University, South Africa