I understand it is useful to preallocate a vector or a matrix, because they are always stored in a contiguous memory block.
However, in terms of a list, it can contains elements of difference length and modes. So my first guess is that a list might merely contains pointers to the true address of its elements. Am I correct? A related question here What is the internal implementation of lists? It says the list is essentially an array, but it does't cover how the elements are stored in a list while size of each element may change.
Example 1: If a list contains a,b,c,d,e
, and when myList$a<-1:1000000
, is the list modified in place (which means only a
is updated) or the whole list are copied and updated?
Example 2
> system.time( { myList <- list()
+ myList$a <- 1:10000000
+ myList$b <- 1:10000100
+ myList$c <- 1:10000200
+ myList$d <- 1:10000300})
user system elapsed
0.01 0.02 0.03
> system.time({ myList2<-list(a=1:10000000,b=1:10000100,c=1:10000200,d=1:10000300) })
user system elapsed
0.00 0.03 0.03
would myList
be very inefficient compared to myList2
due to no preallocation? Or no noticeable performance difference at all no matter how large a,b,c,d
are because the first one copies only pointers a few times more?
Here comes to preallocation. How does it look like for a list? Does it only preallocate the memory for the pointers? If that's the case, I don't see any use since there won't be much data copying for pointers anyway.
> system.time( { myList <- vector("list", 4)
+ myList[[1]] <- 1:10000000
+ myList[[2]] <- 1:10000100
+ myList[[3]] <- 1:10000200
+ myList[[4]] <- 1:10000300
+ names(myList) <- letters[1:4]})
user system elapsed
0.01 0.02 0.03