R/bench.r

Defines functions is_benchmarking benchmark git_sha bench

# benchmarking <- FALSE
bench <- function(name, expr) {
  dur <- system.time(expr)
  if (!is_benchmarking()) {
    print(dur)
    return()
  }

  out <- data.frame(
    file = basename(.bench_path),
    name = name,
    time = Sys.time(),
    sha = git_sha(),
    signif(t(as.matrix(dur)[1:3, 1]))
  )
  append <- file.exists("timings.csv")
  write.table(out, "timings.csv", sep = ",", row = FALSE, append = append,
    col = !append)
}

git_sha <- function() {
  strsplit(system("git log -1 --oneline", intern=T), " ")[[1]][1]
}

benchmark <- function(path) {
  .bench_path <<- path
  on.exit(.bench_path <<- NULL)

  source(path, chdir = TRUE)
}

.bench_path <- NULL
is_benchmarking <- function() {
  !is.null(.bench_path)
}
hadley/bench documentation built on May 14, 2017, 6:11 p.m.