Skip to content

Given an block of expressions in {} workout() individually times each expression in the group. workout_expressions() is a lower level function most useful when reading lists of calls from a file.

Usage

workout(expr, description = NULL)

workout_expressions(exprs, env = parent.frame(), description = NULL)

Arguments

expr

one or more expressions to workout, use {} to pass multiple expressions.

description

A name to label each expression, if not supplied the deparsed expression will be used.

exprs

A list of calls to measure.

env

The environment in which the expressions should be evaluated.

Examples

workout({
  x <- 1:1000
  evens <- x %% 2 == 0
  y <- x[evens]
  length(y)
  length(which(evens))
  sum(evens)
})
#> # A tibble: 6 × 3
#>   exprs                 process     real
#>   <bch:expr>           <bch:tm> <bch:tm>
#> 1 x <- 1:1000            3.13µs   5.48µs
#> 2 evens <- x%%2 == 0     23.6µs  24.56µs
#> 3 y <- x[evens]          4.95µs   5.96µs
#> 4 length(y)              1.12µs   1.91µs
#> 5 length(which(evens))   5.72µs   6.44µs
#> 6 sum(evens)             2.31µs    3.1µs

# The equivalent to the above, reading the code from a file
workout_expressions(as.list(parse(system.file("examples/exprs.R", package = "bench"))))
#> # A tibble: 6 × 3
#>   exprs                 process     real
#>   <bch:expr>           <bch:tm> <bch:tm>
#> 1 x <- 1:1000            2.47µs   3.81µs
#> 2 evens <- x%%2 == 0    19.31µs  20.27µs
#> 3 y <- x[evens]          5.98µs   7.15µs
#> 4 length(y)               1.1µs   1.91µs
#> 5 length(which(evens))    5.2µs    6.2µs
#> 6 sum(evens)             2.22µs    3.1µs