[R] List assignment in a while loop and timing

Saptarshi Guha saptarshi.guha at gmail.com
Mon Mar 30 17:24:55 CEST 2009

Hello R users
I have question about the time involved in list assignment.
Consider the following code snippet(see below). The first line creates
a reader object,
which is the interface to 1MM key-value pairs (serialized R objects) spanning 50
files (a total of 50MB). rhsqstart initiates the reading and I loop, reading
each key-value pair using rhsqnextKVR. If this returns NULL, we switch to the
next file and if this returns null we break.

If I comment out line A1, it takes 39 seconds on a quad core intel with
16GB ram running R-2.8
If I include the assignment A1 it takes ~85 seconds.

I have preassigned the list in line A0, so I'm guessing there is no resizing
going on, so why does the time increase so much?

Thank you for your time.

rdr <- rhsqreader("~/tmp/pp",local=T,pattern="^p")
rdr <- rhsqstart(rdr)
i <- 1;
h=as.list(rep(1,1e6)) ##A0
  value <-rhsqnextKVR(rdr) ##Returns a list of two elements K,V
  if(is.null(value)) {
    rdr <- rhsqnextpath(rdr)
    if(is.null(rdr)) break;
  h[[i]] <- value; ##A1
  i <- i+1

More information about the R-help mailing list