[R] filling a list faster

jim holtman jholtman at gmail.com
Fri Jul 13 13:00:21 CEST 2007


It all depends on what you want to do.  In your example, it is faster
to first fill in a matrix and then convert the matrix to a list.  The
problem with filling in the list is that you are dynamically
allocating space for each iteration which is probably taking at least
an order of magnitude more time than the calculations you are doing.
So I just translated your problem into two steps and it takes about 2
seconds on my system.


> # fill in a matris
> l <- matrix(ncol=3, nrow=10^5)
> system.time(for(i in (1:10^5)) l[i,] <- c(i,i+1,i))
   user  system elapsed
   1.06    0.00    1.10
> # convert to a list
> system.time(l.list <- lapply(1:10^5, function(i) l[i,]))
   user  system elapsed
   0.45    0.00    0.46
> l.list[1:10]
[[1]]
[1] 1 2 1

[[2]]
[1] 2 3 2

[[3]]
[1] 3 4 3

[[4]]
[1] 4 5 4

[[5]]
[1] 5 6 5





On 7/13/07, Balazs Torma <torma at sztaki.hu> 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list