# This is the R code stripped from the 5profile.pdf handout. system.time(Sys.sleep(3)) system.time(readLines("http://imdb.com/top250")) system.time({n=1000000; x=rnorm(n); y=2*x+3+rnorm(n,0,.1); m=lm(y~x)}) x = runif(50) system.time(sqrt(x)) n = 1000000 system.time({ for (i in seq_len(n)) { sqrt(x) } }) require("microbenchmark") a=2 b = microbenchmark(2 + 2, 2 + a, sqrt(x), x ^ .5) str(b) head(as.data.frame(b), n=15) print(b) tapply(X=b$time, INDEX=b$expr, FUN=summary) boxplot(b) require("ggplot2"); autoplot(b) # ggplot2 is a graphics package n=10000; m=matrix(data=as.numeric(1:(n^2)), nrow=n, ncol=n) system.time(s <- apply(m, 1, sum)) system.time(rs <- rowSums(m)) baby.sapply = function(X, FUN) { n = length(X) values.FUN = numeric(n) for (i in seq_len(n)) { values.FUN[i] = FUN(X[i]) } return(values.FUN) } x = rnorm(n=10000) microbenchmark(baby.sapply(X=x, FUN=abs)) baby.sapply.compiled = compiler::cmpfun(baby.sapply) # Compilation here. microbenchmark(baby.sapply.compiled(X=x, FUN=abs)) x.file = "x.RData" if (file.exists(x.file)) { print("loading") load(file=x.file) } else { x = data.frame(height=1:3, weight=4:6) # ... or read 250 web pages, etc. ... save(x, file=x.file) }