Skip to content

malloc problem R2.6.1 on leopard

1 message · Hervé Pagès

#
Hi Hans,

In the case you plan to use the result of allsubsets(n) to loop over the
rows of the big returned matrix, then I would rather recommend a more
efficient approach where you don't generate this matrix at all. Something
like this:

nextSubset <- function(subset)
{

    i <- which(!subset)[1]
    if (is.na(i))
        return(NULL)
    subset[seq_len(i-1)] <- FALSE
    subset[i] <- TRUE
    subset
}

subset <- logical(30)
while (TRUE) {
    ... do something with 'subset' ...
    subset <- nextSubset(subset)
    if (is.null(subset))
        break
}

With an empty loop, I get the following:

  > subset <- logical(20)
  > system.time(while (1) { subset <- nextSubset(subset); if (is.null(subset)) break })
     user  system elapsed
   18.241   0.032  18.735

So with n = 30, this would take between 5 and 6 h just to loop over an
empty loop. This is a lot of time but at least you don't generate all
the possible subsets in advance which is just a waste of memory.

Cheers,
H.
Hans Kestler wrote: