I tested two new variants (the functions iloop()
and cloop()
):
# https://www.bioconductor.org/packages/release/bioc/html/RBGL.html
# if (!requireNamespace("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
#
# BiocManager::install("RBGL")
# BiocManager::install("gRbase")
library("gRbase")
library("proxy") ## proxy::dist()
library("microbenchmark")
#df2can be thought of as (thanks to @Oliver):
df2 <- as.data.frame(replicate(20, rnorm(10)))
names(df2) <- LETTERS[1:20]
comb <- combnPrim(df2,5, simplify = TRUE)
ori <- function(comb) {
range <- length(comb)/5
result_vector <- vector(mode="list",length = range )
for (i in seq(range))
{
total <- as.numeric(0)
for ( j in seq(4))
{
for ( k in seq(j+1,5))
{
diff <- sum( ( mapply( '/',unlist( comb[,i][j] ) - unlist( comb[,i][k] ), ( unlist(comb[,i][j] ) + unlist( comb[,i][k] )) / 2 )^2))
total = total + diff
}
}
result_vector[[i]] <- total
}
return(result_vector)
}
nomapply <- function(comb) {
range <- ncol(comb) ## length(comb)/5
result_vector <- vector(mode="list",length = range )
for (i in seq(range)) {
total <- as.numeric(0)
for ( j in seq(4)) for ( k in seq(j+1,5)) {
diff <- sum( ( (unlist( comb[,i][j] ) - unlist( comb[,i][k] )) /
( unlist(comb[,i][j] ) + unlist( comb[,i][k] )) / 2 )^2)
total = total + diff
}
result_vector[[i]] <- total
}
return(result_vector)
}
ind <- function(comb) {
range <- ncol(comb) ## length(comb)/5
result_vector <- vector(mode="list",length = range )
for (i in seq(range)) {
total <- as.numeric(0)
for ( j in seq(4)) for ( k in seq(j+1,5)) {
diff <- sum( ( (unlist( comb[j,i] ) - unlist( comb[j,i] )) /
( unlist(comb[j,i] ) + unlist( comb[k,i] )) / 2 )^2)
total = total + diff
}
result_vector[[i]] <- total
}
return(result_vector)
}
nounlist <- function(comb) {
range <- ncol(comb) ## length(comb)/5
result_vector <- vector(mode="list",length = range )
for (i in seq(range)) {
total <- as.numeric(0)
for ( j in seq(4)) for ( k in seq(j+1,5)) {
diff <- sum( ( (comb[j,i][[1]] - comb[j,i][[1]]) / ( comb[j,i][[1]] + comb[k,i][[1]]) / 2 )^2)
total = total + diff
}
result_vector[[i]] <- total
}
return(result_vector)
}
range <- ncol(comb) ## length(comb)/5
fn_dist <- function(x, y) sum(((x-y) / ((x+y) / 2))^2)
iloop <- function(i) {
total <- as.numeric(0)
for ( j in seq(4)) {
for ( k in seq(j+1,5)) {
diff <- fn_dist(comb[j,i][[1]], comb[k,i][[1]])
total = total + diff
}
}
return(total)
}
# result_vector <- sapply(1:range, iloop)
cloop <- function(ci) {
total <- as.numeric(0)
for ( j in seq(4)) {
for ( k in seq(j+1,5)) {
diff <- fn_dist(ci[j][[1]], ci[k][[1]])
total = total + diff
}
}
return(total)
}
# result_vector <- apply(comb, 2, cloop)
# r <- apply(comb,2, function(l) sum(proxy::dist(method = fn_dist, do.call(rbind,l))))
microbenchmark(orig=ori(comb), orig2=nomapply(comb), orig3=ind(comb), orig4=nounlist(comb),
iloop=sapply(1:range, iloop), cloop=apply(comb, 2, cloop), unit = "relative",
proxy=apply(comb,2, function(l) sum(proxy::dist(method = fn_dist, do.call(rbind,l)))),
times=10)
These are the results:
# > microbenchmark(orig=ori(comb), orig2=nomapply(comb), orig3=ind(comb), orig4=nounlist(comb),
# + iloop=sapply(1:range, iloop), cloop=apply(comb, 2, cloop), unit = "relative",
# + proxy=apply(comb,2, function(l) sum(proxy::dist(method = fn_dist, do.call(rbind,l)))),
# + times=10)
# Unit: relative
# expr min lq mean median uq max neval cld
# orig 8.647526 8.648012 8.429268 8.597876 8.551316 7.1967369 10 e
# orig2 2.613248 2.627175 2.564267 2.612007 2.633428 2.1851621 10 d
# orig3 1.949486 1.969982 1.911910 1.933789 1.963484 1.6318174 10 b
# orig4 1.000000 1.000000 1.000000 1.000000 1.000000 1.0000000 10 a
# iloop 1.127511 1.146384 1.118755 1.149810 1.140409 0.9477470 10 a
# cloop 1.137061 1.154385 1.128315 1.149292 1.143234 0.9702812 10 a
# proxy 2.142964 2.127388 2.078447 2.100761 2.067607 1.9183790 10 c
The little changes in the inner loop gave the most performance gain:
- using
/
for the vectors (no mapply()
)
- compacting the indexing (no double indexing) and
- using
...[[1]]
instead of unlist()
.
To have clear code I would prefer the variant cloop()
or using proxy::dist()
.