Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.