On 08/09/11 12:05, William Dunlap wrote:
You did not show the code you used to populate your
object, but consider the following ways to make as.list(1:50000)
via repeated assignments:
> system.time( { z0<- list() ; for(i in 1:50000)z0[i]<- list(i) } )
user system elapsed
13.34 0.00 13.42
> system.time( { z1<- list() ; for(i in 1:50000)z1[[i]]<- i } )
user system elapsed
12.98 0.00 13.36
> system.time( { z2<- vector("list",50000) ; for(i in 1:50000)z2[i]<-
list(i) } )
user system elapsed
0.28 0.00 0.26
> system.time( { z3<- vector("list",50000) ; for(i in 1:50000)z3[[i]]<- i } )
user system elapsed
0.22 0.00 0.20
> identical(z0,z1)&& identical(z0,z2)&& identical(z0,z3)
[1] TRUE
Preallocating a vector to its ultimate size can be much faster than
repeatedly expanding it.
This is *very* interesting to me. Years ago I learned to use the ``z1''
syntax,
and thought I was being cool. It's stunning how much more efficient the
``z2'' and ``z3'' syntaxes are. I'll bet that there are many bunnies
like me out
there who are unaware of the advantages of preallocation via
vector("list",...).
Thanks for the enlightenment.
cheers,
Rolf Turner
______________________________________________
R-help@r-project.org 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.