Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created December 15, 2017 06:36
Show Gist options
  • Save njtierney/5d1eec9bf9e79e1a14090c01b4302907 to your computer and use it in GitHub Desktop.
Save njtierney/5d1eec9bf9e79e1a14090c01b4302907 to your computer and use it in GitHub Desktop.
This gist documents my attempts to "kinda tidily" do a speed comparison of functions
# define functions for the speedcheck
library(magrittr)
bench_funs <- function(){
microbenchmark::microbenchmark(
miss_case_pct = miss_case_pct(airquality),
miss_case_prop = miss_case_prop(airquality),
miss_case_summary = miss_case_summary(airquality),
miss_case_table = miss_case_table(airquality),
miss_prop_summary = miss_prop_summary(airquality),
miss_var_pct = miss_var_pct(airquality),
miss_var_prop = miss_var_prop(airquality),
miss_var_run = miss_var_run(airquality, Ozone),
miss_var_span = miss_var_span(airquality, Solar.R, 2),
miss_var_summary = miss_var_summary(airquality),
miss_var_table = miss_var_table(airquality),
gg_miss_case = gg_miss_case(airquality),
gg_miss_fct = gg_miss_fct(riskfactors,fct = marital),
gg_miss_var = gg_miss_var(airquality),
gg_miss_span = gg_miss_span(pedestrian,
hourly_counts,
span_every = 3000)
)
}
# tidy up the benchmark output into a tibble
tidy_mb <- function(mb_obj){
tibble::as_tibble(mb_obj) %>%
dplyr::arrange(expr) %>%
dplyr::group_by(expr) %>%
dplyr::mutate(row_id = 1:n()) %>%
dplyr::ungroup()
}
# make sure I am on master branch using Jenny's awesome githug
# https://github.com/jennybc/githug
# devtools::install_github("jennybc/githug")
githug::git_switch(name = "master")
# make sure that the current version of the package is loaded
devtools::load_all()
mb_old_funs <- bench_funs()
# save this output
readr::write_rds(x = mb_old_funs,
path = "~/Downloads/mb_old_master.rds")
# change the branch
githug::git_switch(name = "rowMeans-isna")
# make sure that I load the new package into memory
devtools::load_all()
mb_new_funs <- bench_funs()
# save this output
readr::write_rds(x = mb_new_funs,
path = "~/Downloads/mb_new_row_means.rds")
mb_old_funs <- readr::read_rds("~/Downloads/mb_old_master.rds")
mb_new_funs <- readr::read_rds("~/Downloads/mb_new_row_means.rds")
tidy_mb_old <- tidy_mb(mb_old_funs) %>% dplyr::mutate(type = "old")
tidy_mb_new <- tidy_mb(mb_new_funs) %>% dplyr::mutate(type = "new")
mb_combined <- dplyr::bind_rows(tidy_mb_old,
tidy_mb_new)
library(ggplot2)
mb_combined %>%
ggplot(aes(x = expr,
y = time,
colour = type)) +
geom_boxplot() +
coord_flip()
# let's focus on the ones that have a more noticeable difference
mb_combined %>%
dplyr::filter(expr %in% c("gg_miss_case",
"miss_var_table",
"miss_var_summary",
"miss_var_span",
"miss_case_table",
"miss_case_summary")) %>%
ggplot(aes(x = expr,
y = time,
colour = type)) +
geom_boxplot() +
coord_flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment