The goal of bench is to benchmark code, tracking execution time, memory allocations and garbage collections.

Installation

You can install the development version from GitHub with:

Features

bench::mark() is used to benchmark one or a series of expressions, we feel it has a number of advantages over alternatives.

  • Always uses the highest precision APIs available for each operating system (often nanoseconds).
  • Tracks memory allocations for each expression.
  • Tracks the number and type of R garbage collections per expression iteration.
  • Verifies equality of expression results by default, to avoid accidentally benchmarking inequivalent code.
  • Has bench::press(), which allows you to easily perform and combine benchmarks across a large grid of values.
  • Uses adaptive stopping by default, running each expression for a set amount of time rather than for a specific number of iterations.
  • Expressions are run in batches and summary statistics are calculated after filtering out iterations with garbage collections. This allows you to isolate the performance and effects of garbage collection on running time (for more details see Neal 2014).

The times and memory usage are returned as custom objects which have human readable formatting for display (e.g. 104ns) and comparisons (e.g. x$mem_alloc > "10MB").

There is also full support for plotting with ggplot2 including custom scales and formatting.

Usage

bench::mark()

Benchmarks can be run with bench::mark(), which takes one or more expressions to benchmark against each other.

bench::mark() will throw an error if the results are not equivalent, so you don’t accidentally benchmark inequivalent code.

Results are easy to interpret, with human readable units.

By default the summary uses absolute measures, however relative results can be obtained by using relative = TRUE in your call to bench::mark() or calling summary(relative = TRUE) on the results.

bench::press()

bench::press() is used to run benchmarks against a grid of parameters. Provide setup and benchmarking code as a single unnamed argument then define sets of values as named arguments. The full combination of values will be expanded and the benchmarks are then pressed together in the result. This allows you to benchmark a set of expressions across a wide variety of input sizes, perform replications and other useful tasks.

set.seed(42)

create_df <- function(rows, cols) {
  as.data.frame(setNames(
    replicate(cols, runif(rows, 1, 1000), simplify = FALSE),
    rep_len(c("x", letters), cols)))
}

results <- bench::press(
  rows = c(10000, 100000),
  cols = c(10, 100),
  {
    dat <- create_df(rows, cols)
    bench::mark(
      min_iterations = 100,
      bracket = dat[dat$x > 500, ],
      which = dat[which(dat$x > 500), ],
      subset = subset(dat, x > 500)
    )
  }
)
#> Running with:
#>     rows  cols
#> 1  10000    10
#> 2 100000    10
#> 3  10000   100
#> 4 100000   100
results
#> # A tibble: 12 x 12
#>    expression   rows  cols      min     mean   median      max `itr/sec` mem_alloc  n_gc n_itr total_time
#>    <chr>       <dbl> <dbl> <bch:tm> <bch:tm> <bch:tm> <bch:tm>     <dbl> <bch:byt> <dbl> <int>   <bch:tm>
#>  1 bracket     10000    10 924.67µs  955.8µs 942.89µs    2.4ms    1046.     1.13MB    11   480   458.78ms
#>  2 which       10000    10 616.26µs 633.57µs 627.55µs 758.16µs    1578.    572.7KB     8   736   466.31ms
#>  3 subset      10000    10   1.05ms   1.08ms   1.07ms   2.56ms     923.     1.25MB    12   419      454ms
#>  4 bracket    100000    10  12.92ms  13.21ms  13.05ms  15.59ms      75.7   11.16MB    42    58   765.92ms
#>  5 which      100000    10   9.06ms   9.37ms   9.19ms  11.56ms     107.     5.44MB    17    83   777.74ms
#>  6 subset     100000    10  13.83ms  13.94ms   13.9ms  15.58ms      71.7    12.3MB    42    58   808.39ms
#>  7 bracket     10000   100   6.68ms   6.89ms   6.89ms    7.1ms     145.     9.68MB    38    62   427.42ms
#>  8 which       10000   100   3.37ms   3.54ms   3.54ms   3.75ms     282.     3.96MB    15   116   410.86ms
#>  9 subset      10000   100   6.71ms   6.99ms   6.97ms   8.79ms     143.      9.8MB    29    71   496.54ms
#> 10 bracket    100000   100  66.28ms  71.46ms  67.41ms 101.47ms      14.0   97.09MB    82    20      1.43s
#> 11 which      100000   100  27.09ms  29.04ms  27.36ms  45.75ms      34.4   39.84MB    34    66      1.92s
#> 12 subset     100000   100  67.04ms  70.04ms  67.32ms  85.08ms      14.3   98.24MB    78    23      1.61s

Plotting

ggplot2::autoplot() can be used to generate an informative default plot. This plot is colored by gc level (0, 1, or 2) and faceted by parameters (if any). By default it generates a beeswarm plot, however you can also specify other plot types (jitter, ridge, boxplot, violin). See ?autoplot.bench_mark for full details.

You can also produce fully custom plots by un-nesting the results and working with the data directly.

system_time()

bench also includes system_time(), a higher precision alternative to system.time().