基本的矢量化使用ifelse
似乎本身就获得了 40% 的加速:
n <- 300
A <- rnorm(n)
val_1 <- rpois(n, 10)
val_2 <- rpois(n, 20)
tempMatrix <- matrix(NA, n, n)
tempMatrix2 <- matrix(NA, n, n)
foo <- function(i, j) if( i %% j == 0) 5 else i
microbenchmark(times=30, orig=
for(i in 1:n) {
for(j in 1:n)
{
if (A[i] < A[j]) {
tempMatrix[i, j] <- foo(val_1[i], val_2[j])
} else {
tempMatrix[i, j] <- foo(val_2[j], val_1[i])
}
}
}
,mapp=
for(j in 1:n) {
w <- A < A[j]
x <- ifelse(w, val_1, val_2[j])
y <- ifelse(w, val_2[j], val_1)
tempMatrix2[,j] = mapply(foo, x, y)
}
)
all.equal(tempMatrix, tempMatrix2)
产量:
Unit: milliseconds
expr min lq mean median uq max neval
orig 187.449344 191.274325 195.7138641 192.635209 196.705712 220.292461 30
mapp 116.479130 117.531128 121.5658793 118.779243 123.359916 147.540282 30
将 n 提高到 400 产生:
Unit: milliseconds
expr min lq mean median uq max neval
orig 338.356754 341.815807 351.2388823 345.4547705 357.107122 382.312691 30
mapp 203.892420 206.377714 209.9775747 208.6823080 210.757181 239.354271 30