loop ↔ vectorized ↔ direct [Two-Stage / GS Designs]

Hi ElMaestro,

» vecQT1=c(1:5000)
» vecQT2=c(1:5000)
» for (i in 1:5000)
»    {
»     vecQT1[i]=qt(p=1-alpha1, df=i)
»     vecQT2[i]=qt(p=1-alpha2, df=i)
»    }

Later down you makes calls to vecQT1[df] and vecQT2[df]. That’s awfully slooow. Why not simply call qt(p=foo, df=bar) directly?

library(microbenchmark) loop <- function(alpha1, alpha2, df) {   vecQT2 <- vecQT1 <- numeric()   for (j in 1:5000) {     vecQT1[j]=qt(p=1-alpha1, df=j)     vecQT2[j]=qt(p=1-alpha2, df=j)   }   return(QT=c(vecQT1[df], vecQT2[df])) } vectorized <- function(alpha1, alpha2, df) {   vecQT1 <- qt(p=1-alpha1, df=1:5000)   vecQT2 <- qt(p=1-alpha2, df=1:5000)   return(QT=c(vecQT1[df], vecQT2[df])) } direct <- function(alpha1, alpha2, df) {   return(QT=c(qt(p=1-alpha1, df=df,               qt(p=1-alpha2, df=df)))) } res <- microbenchmark(loop(0.0294, 0.05, 10),                       vectorized(0.0294, 0.05, 10),                       direct(0.0294, 0.05, 10), times=200L,                       control=list("random", warmup=10)) print(res)

Dif-tor heh smusma 🖖
Helmut Schütz

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes