Created
March 20, 2019 10:06
-
-
Save echasnovski/65821e47f56f5a9f93498b72b520354b to your computer and use it in GitHub Desktop.
Benchmarks for different types of "unsorting"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(microbenchmark) | |
unsort_prealloc <- function(xo, ord) { | |
xu <- character(length(xo)) | |
xu[ord] <- xo | |
xu | |
} | |
unsort_reorder <- function(xo, ord) { | |
xo[order(ord)] | |
} | |
bench_unsort <- function(n) { | |
x <- sample(letters, n, replace = TRUE) | |
ord <- order(x) | |
xo <- x[ord] | |
microbenchmark( | |
prealloc = unsort_prealloc(xo, ord), | |
reorder = unsort_reorder(xo, ord), | |
times = 1000 | |
) %>% | |
group_by(expr) %>% | |
summarise(med_time = median(time / 1e3)) | |
} | |
set.seed(101) | |
benches <- tibble(n = floor(seq(10, 10000, length.out = 100))) %>% | |
mutate(bench = map(n, bench_unsort)) %>% | |
unnest(bench) | |
benches %>% | |
ggplot(aes(n, med_time, color = expr)) + | |
geom_line() + | |
labs(x = "Vector length", y = "Median time (microseconds)") + | |
guides(color = guide_legend(title = NULL)) + | |
theme_bw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment