Ok, so I tried out @Math suggestion and decided to time it using the following code:
# Clear workspace
rm(list = ls())
# Reproducible results
set.seed(42)
# Set dimensions
n1 = 500
n2 = 150
n3 = 100
# Create matrices
A = matrix(rnorm(n1*n2),nrow=n1,ncol=n2)
B = matrix(rnorm(n2*n3),nrow=n2,ncol=n3)
# Assign row/col names
rownames(A)=paste("Arow",seq(1,nrow(A)),sep="")
colnames(A)=paste("Acol",seq(1,ncol(A)),sep="")
rownames(B)=paste("Brow",seq(1,nrow(B)),sep="")
colnames(B)=paste("Bcol",seq(1,ncol(B)),sep="")
# State number of correlations to be performed
cat(paste("Total number of correlations =",nrow(A)*ncol(B),"\n"))
# Test 1 using rbind()
cat("Starting test 1 with rbind()\n")
ptm = proc.time()
output = c()
for( i in 1:nrow(A) ){
for( j in 1:ncol(B) ){
myTest = cor.test(A[i,],B[,j],method="spearman")
output = rbind(output,c(rownames(A)[i],colnames(B)[j],
myTest$p.value,myTest$estimate))
}
}
print(proc.time() - ptm)
# Test 2 using pre-built matrix
cat("Starting test 2 with pre-built matrix\n")
ptm = proc.time()
output = matrix(0, nrow=nrow(A)*ncol(B), ncol=4)
count = 1
for( i in 1:nrow(A) ){
for( j in 1:ncol(B) ){
myTest = cor.test(A[i,],B[,j],method="spearman")
output[count,] = c(rownames(A)[i],colnames(B)[j],
myTest$p.value,myTest$estimate)
count = count + 1
}
}
print(proc.time() - ptm)
Running this code, produces the following results:
Total number of correlations = 50000
Starting test 1 with rbind()
user system elapsed
275.560 6.963 282.913
Starting test 2 with pre-built matrix
user system elapsed
29.869 0.218 30.114
So evidently there is quite a difference, ac. I was not aware of this 'problem' with using the rbind() function to gradually build matrices. Thank you @Math for pointing this out! :-)
Cheers!