Skip to content

Instantly share code, notes, and snippets.

@mkiang
Created June 10, 2015 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkiang/1b0822b7d323a5c7036a to your computer and use it in GitHub Desktop.
Save mkiang/1b0822b7d323a5c7036a to your computer and use it in GitHub Desktop.
Benchmark and plot four different ways to generate sequences in R
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