# microbenchmarking with R

April 28, 2012
By

(This article was first published on TRinker's R Blog » R, and kindly contributed to R-bloggers)

I love to benchmark.  Maybe I’m a bit weird but I love to bench  everything in R.  Recently I’ve had people raise accuracy challenges to the typical system.time and rbenchmark package approaches to benchmarking.  I saw Hadley Wickham promoting the package microbenchmarking and decided to give it a whirl.  This approach claims to improve accuracy and adjusts to your OS.  A nice box plot or a ggplot of the functions output can also aid in understanding and comparing functions.  Here’s a demo test:

```library(microbenchmark); library(plyr)
op <- microbenchmark(
PLYR=ddply(mtcars, .(cyl, gear), summarise,
output = mean(hp)),
AGGR=aggregate(hp ~ cyl + gear, mtcars, mean),
TAPPLY = tapply(mtcars\$hp, interaction(mtcars\$cyl,
mtcars\$gear), mean),
times=1000L)

print(op) #standard data frame of the output
boxplot(op) #boxplot of output
library(ggplot2) #nice log plot of the output
qplot(y=time, data=op, colour=expr) + scale_y_log10()```

The output to the console window using print(op) yields like this:

```Unit: milliseconds
expr      min       lq   median       uq       max
1   AGGR 2.856758 2.972932 3.121999  3.48615 121.49828
2   PLYR 7.880229 8.497956 8.983880 10.71436 139.04940
3 TAPPLY 1.108085 1.159873 1.196731  1.30824  67.33326```

The ggplot log plot from the output:

ggplot2 Plot of the Output

The boxplot from output:

Box Plot of the Output

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Tags: , , ,