Skip to content
Prev 305688 / 398506 Next

Possible Improvement of the R code

Getting rid of one more matrix subscripting call trims the
time by another 10-20%.  With the following function the times
for 10^4 reps for a a 11-row input matrix become
      raw compiled
  f1 5.57     3.04
  f3 3.73     2.14
  f5 3.45     1.95
  f6 1.63     0.90
  f7 1.32     0.81

f7 <- function(param) {
    prev <- param[1,]
    for(i in 2:nrow(param)) {
        # do vector and matrix subscripting only once/iteration
        # and repeated sums only once/iteration.
        p1 <- prev[1]
        p2 <- prev[2]
        p42 <- prev[4]/2
        p342 <- prev[3] + p42
        p425 <- p42 + prev[5]
        param[i,] <- prev <- c(
            p342,
            p425,
            p1 * p342,
            p1 * p425 + p2 * p342,
            p2 * p425)
    }
    param
}
f7c <- cmpfun(f7)




Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com