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

posted by Helmut Homepage – Vienna, Austria, 2017-08-20 14:40  – Posting: # 17719
Views: 23,382

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)


Cheers,
Helmut Schütz
[image]

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

Complete thread:

Activity
 Admin contact
20,135 posts in 4,245 threads, 1,385 registered users;
online 8 (0 registered, 8 guests [including 2 identified bots]).
Forum time (Europe/Vienna): 02:46 CET

In the beginner’s mind there are many possibilities.
In the expert’s mind there are few.    Shunryu Suzuki

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5