Skip to content

Instantly share code, notes, and snippets.

@aavogt
Created November 17, 2024 21:09
Show Gist options
  • Save aavogt/b882e069d1e47b6c85c9fc267ace2632 to your computer and use it in GitHub Desktop.
Save aavogt/b882e069d1e47b6c85c9fc267ace2632 to your computer and use it in GitHub Desktop.
benchmarking ghc (`elem` "{([") vs. (\c -> c `elem` "{([") vs. ...
library(pacman)
p_load(tidyverse, tictoc, glue, e1071, directlabels, cache)
split <- function(x) unlist(strsplit(x, ""))
sample(c(letters, split("{}[]()")), 10^5, replace = TRUE) %>% as.list() %>% do.call(str_c, .) %>% writeLines("input0.txt")
# map_chr(commands, function(command) readLines(paste0(command, ".hs"))) %>% setNames(commands) %>% deparse %>% writeLines
mains <- c(
"./listEtaEx" = "main = print . length . filter (\\c -> c `elem` ['{', '(', '[']) =<< readFile \"input.txt\"",
"./list" = "main = print . length . filter (`elem` ['{', '(', '[']) =<< readFile \"input.txt\"",
"./string" = "main = print . length . filter (`elem` \"{([\") =<< readFile \"input.txt\"",
"./stringEtaEx" = "main = print . length . filter (\\c -> c `elem` \"{([\") =<< readFile \"input.txt\""
)
iwalk(mains, ~ {
writeLines(.x, glue("{.y}.hs"))
system(glue("ghc -O2 {.y}.hs"))
})
commands <- Sys.glob("*.hs") %>% str_remove(".hs$") %>% paste0("./", .)
pmat <- permutations(length(commands))
cache(tictocs =
map_dfr(log(seq(exp(5), exp(7), length.out = 7)), function(b) {
system(glue("cat {paste(rep('input0.txt', round(10^ (b-5))), collapse = ' ')} > input.txt"))
map_dfr(1, function(replicate) {
map_dfr(sample(nrow(pmat), 10), function(permu) {
map_dfr(pmat[permu, ], function(i) {
tic()
system(commands[i])
r <- toc()
time <- with(r, toc - tic)
tibble(time = time, com = commands[i], b = b, n = 10^b, replicate = replicate)
})
})
})
})
)
tictocs <- within(tictocs, {
str <- str_detect(tictocs$com, "string")
etaEx <- str_detect(tictocs$com, "EtaEx")
opts <- ifelse(str, "string", "list")
label <- ifelse(etaEx, "\\ c -> c `elem` _", "(`elem` _)")
})
plot(
ggplot(tictocs, aes(b, time, label = label, group = com)) +
geom_point(aes(col = opts)) +
theme_minimal() +
geom_dl(method = "last.qp", cex = 8) +
geom_smooth(aes(col = opts)) +
scale_color_manual(name = "_ =", values = c("red", "blue")) +
xlim(5, 8)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment