[R] Very slow assignments

William Dunlap wdunlap at tibco.com
Thu Sep 8 02:05:29 CEST 2011


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.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of André Rossi
> Sent: Wednesday, September 07, 2011 3:08 PM
> To: r-help at r-project.org
> Subject: [R] Very slow assignments
> 
> I'm creating an object of a S4 class that has two slots: ListExamples, which
> is a list, and idx, which is an integer (as the code below).
> 
> Then, I read a data.frame file with 10000 (ten thousands) of lines and 10
> columns, do some pre-processing and, basically, I store each line as an
> element of a list in the slot ListExamples of the S4 object. However, any
> kind of assignment operation (<-) that I try to do after this took a
> considerable time.
> 
> Can anyone explain me why dois it happen? Is it possible to speed up an
> script that deals with a big number of data (it might be data.frame or
> list)?
> 
> Thank you,
> 
> André Rossi
> 
> setClass("Buffer",
>     representation=representation(
>         Listexamples = "list",
>         idx = "integer"
>     )
> )
> 
> 	[[alternative HTML version deleted]]



More information about the R-help mailing list