setwd("C:/Users/Ken/Desktop/SISG/SISG-13/") ## session 6 # Some profiler examples # - use Rprof(filename), Rprof(NULL) to profile them # With more careful coding, we can make the calculation much quicker Rprof("exampletiming.txt") # turn it on - then do some work Rprof(NULL) # turn it off! summaryRprof("exampletiming.txt") # summary system.time({ my.sims <- replicate(10000, { mydiff <- rnorm(100, 2, 7) t.test(mydiff)$p.value }) }) system.time({ my.sims <- replicate(10000, { mydiff <- rnorm(100, 2, 7) t <- mean(mydiff)/sd(mydiff)*sqrt(100) 2*pt(-abs(t), df=99) }) }) crit.t <- qt(0.975, df=99) system.time({ my.sims <- replicate(10000, { mydiff <- rnorm(100, 2, 7) t <- mean(mydiff)/sd(mydiff)*sqrt(100) abs(t)>crit.t }) }) system.time({ mydiffs <- matrix(rnorm(100*100000,2,7),nrow=100) m <- colMeans(mydiffs) s <- sqrt(colMeans(mydiffs*mydiffs)-m*m) m*sqrt(100)/s > crit.t }) # profiler indicates this won't help much; # sqrt() is not using much CPU time crit2<-crit.t^2/100 system.time({ mydiffs <- matrix(rnorm(100*100000,2,7),nrow=100) m2 <- colMeans(mydiffs)^2 s2 <- colMeans(mydiffs*mydiffs)-m2 m2/s2 > crit2 })