Функция для тестирование кода на разном объёме данных
Пример реализации:
library(microbenchmark)
library(ggplot2)
theme_set(theme_bw())
bench <- function(..., n = seq.int(100, 100000, 100),
gen.fun = runif, times = 100, plot = TRUE) {
set.seed(42)
# capture unevaluated expressions
exprs <- eval(substitute(alist(...)))
nexprs <- length(exprs)
nout <- nexprs * times
# allocate memroy for the results
out <- list(n = integer(nout),
expr = factor(integer(nout), levels = as.character(exprs)),
time = numeric(nout))
message("Start benchmarking...")
pb <- txtProgressBar(0, length(n), style = 3)
for (i in seq_along(n)) {
# define indices
start <- (i - 1) * nexprs + 1
end <- start + nexprs - 1
# generate data
x <- gen.fun(n[i])
# measure timing
res <- microbenchmark(list = exprs, times = times, control = list(order = "block"))
# join results
res <- stack(lapply(split(res$time, res$expr), median.default))
out[["n"]][start:end] <- n[i]
out[["expr"]][start:end] <- res$ind
out[["time"]][start:end] <- res$values
setTxtProgressBar(pb, i)
}
class(out) <- "data.frame"
attr(out, "row.names") <- .set_row_names(length(out$n))
close(pb)
if (plot) {
message("Drawing plot...")
gp <- ggplot(out, aes(x = n, y = time, linetype = expr, color = expr)) +
geom_smooth(se = FALSE)
print(gp)
}
invisible(out)
}
Использование:
mean1 <- function(x) mean(x)
mean2 <- function(x) sum(x) / length(x)
bench(mean1(x), mean2(x))
Результат:
Возможно для генерации данных стоит передавать функцию с доп. параметрами.
