Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Code for GGD blog post on microbenchmark
# see blog post here:
# http://gettinggeneticsdone.blogspot.com/2015/01/microbenchmark-package-r-compare-runtime-r-expressions.html
library(dplyr)
library(nycflights13)
flights
# base
aggregate(flights$arr_delay, by=list(flights$carrier), mean, na.rm=TRUE)
library(sqldf)
sqldf("SELECT carrier, avg(arr_delay) FROM flights GROUP BY carrier")
library(data.table)
flightsDT = data.table(flights)
flightsDT[ , mean(arr_delay, na.rm=TRUE), carrier]
library(dplyr)
flights %>% group_by(carrier) %>% summarize(mean(arr_delay, na.rm=TRUE))
library(microbenchmark)
mbm = microbenchmark(
base = aggregate(flights$arr_delay, by=list(flights$carrier), mean, na.rm=TRUE),
sqldf = sqldf("SELECT carrier, avg(arr_delay) FROM flights GROUP BY carrier"),
datatable = flightsDT[ , mean(arr_delay, na.rm=TRUE), carrier],
dplyr = flights %>% group_by(carrier) %>% summarize(mean(arr_delay, na.rm=TRUE)),
times=50
)
mbm
library(ggplot2)
autoplot(mbm)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment