Skip to content
Prev 163802 / 398506 Next

How to optimize this codes ?

A hastily written recursive version is:

computeB3 <-
function(A) {
   B <- rep(list(integer(0)), ncol(A))
   storage.mode(A) <- "logical"
   recurse <- function(thisCol = 1, includedCols = rep(NA, ncol(A)),
allSoFar = rep(TRUE, nrow(A))) {
      if (thisCol < ncol(A)) # skip this column
          Recall(thisCol+1, replace(includedCols, thisCol, FALSE),
allSoFar = allSoFar)
      else if (thisCol > ncol(A))
          return()
      includedCols[thisCol] <- TRUE
      allSoFar <- allSoFar & A[,thisCol]
      nIncludedCols <- sum(includedCols[1:thisCol])
      # cat(thisCol, ":", nIncludedCols, ":", includedCols[1:thisCol],
sum(allSoFar), "\n")
      # Note B<<- in next line uses lexical scoping to change
computeB3:B.  Does not work in S+.
      B[[nIncludedCols]][length(B[[nIncludedCols]])+1] <<- sum(allSoFar)
      Recall(thisCol+1, includedCols, allSoFar = allSoFar)
   }
   recurse()
   B
}

The elements of B[[i]], for each i, are in a different order than they
are in
the other functions, but the histograms are the same.

I added its time to the above table:

sample.size       time:computeB0   time:computeB1    time:computeB3
       1000                 5.36             0.98              0.12    
      10000                36.82             2.49              0.25
     100000               381.34            18.97              1.62
    1000000                    ?           290.21             16.36

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com