Created
June 10, 2015 00:45
-
-
Save mkiang/1b0822b7d323a5c7036a to your computer and use it in GitHub Desktop.
Benchmark and plot four different ways to generate sequences in R
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(microbenchmark) | |
library(ggplot2) | |
library(dplyr) | |
holder <- NULL | |
endpoints <- 10 ^ (1:9) | |
for(endpoint in endpoints) { | |
x <- microbenchmark( | |
seq_len = seq_len(endpoint), | |
colon = 1:endpoint, | |
seq.int = seq.int(to = endpoint), | |
seq = seq(to = endpoint), | |
unit = "ms" | |
) | |
x$end <- endpoint | |
holder <- rbind(holder, x) | |
} | |
df <- holder %>% | |
group_by(expr, end) %>% | |
summarize(mean = mean(time), | |
median = median(time), | |
p025 = quantile(time, probs = .025), | |
p975 = quantile(time, probs = .975)) | |
p <- ggplot(data = df, | |
aes(x = end, y = mean, color = expr)) + | |
geom_point(size = 4, alpha = .7) + | |
geom_errorbar(aes(ymax = p975, ymin = p025), | |
width = .1, alpha = .7) + | |
scale_x_log10(breaks = 10 ^ (1:9), | |
labels = c(expression(10^1), | |
expression(10^2), | |
expression(10^3), | |
expression(10^4), | |
expression(10^5), | |
expression(10^6), | |
expression(10^7), | |
expression(10^8), | |
expression(10^9)), | |
expand = c(0, .1)) + | |
scale_y_log10(limits = c(10^1, 10^10), | |
breaks = 10 ^ (1:10), | |
labels = c(expression(10^1), | |
expression(10^2), | |
expression(10^3), | |
expression(10^4), | |
expression(10^5), | |
expression(10^6), | |
expression(10^7), | |
expression(10^8), | |
expression(10^9), | |
expression(10^10))) + | |
theme_classic() + annotation_logticks() + | |
theme(legend.justification = c(1, 0), | |
legend.position = c(1, 0), | |
legend.title = element_blank()) + | |
labs(x = "Number of Integers", | |
y = "Milliseconds") | |
ggsave(plot = p, file = "./sequencegenerators.pdf", | |
scale = 1.5, width = 6, height = 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment