[R] filling a list faster

Philippe Grosjean phgrosjean at sciviews.org
Fri Jul 13 13:03:09 CEST 2007


If all the data coming from your iterations are numeric (as in your toy 
example), why not to use a matrix with one row per iteration? Also, do 
preallocate the matrix and do not add row or column names before the end 
of the calculation. Something like:

 > m <- matrix(rep(NA, 3*10^5), ncol = 3)
 > system.time(for(i in (1:10^5)) m[i, ] <- c(i,i+1,i))
    user  system elapsed
   1.362   0.033   1.424

That is, about 1.5sec on my Intel Duo Core 2.33Mhz MacBook Pro, compared to:

 > l <- list("1"<-c(1,2,3))
 > system.time(for(i in (1:10^5)) l[[length(l)+1]] <- c(i,i+1,i))
    user  system elapsed
191.629  49.110 248.454

... more than 4 minutes for your code.

By the way, what is your "very fast machine", that is actually four 
times faster than mine (grrrrr!)?

Best,

Philippe Grosjean

..............................................<∞}))><........
  ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
  ) ) ) ) )
( ( ( ( (    Numerical Ecology of Aquatic Systems
  ) ) ) ) )   Mons-Hainaut University, Belgium
( ( ( ( (
..............................................................

Balazs Torma wrote:
> hello,
> 
>     first I create a list:
> 
> l <- list("1"<-c(1,2,3))
> 
>     then I run the following cycle, it takes over a minute(!) to  
> complete on a very fast mashine:
> 
> for(i in (1:10^5)) l[[length(l)+1]] <- c(i,i+1,i)
> 
> How can I fill a list faster? (This is just a demo test, the elements  
> of the list are calculated iteratively in an algorithm)
> 
> Are there any packages and documents on how to use more advanced and  
> fast data structures like linked-lists, hash-tables or trees for  
> example?
> 
> Thank you,
> Balazs Torma
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list