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.
workout(expr, description = NULL)
workout_expressions(exprs, env = parent.frame(), description = NULL)
one or more expressions to workout, use {}
to pass multiple
expressions.
A name to label each expression, if not supplied the deparsed expression will be used.
A list of calls to measure.
The environment in which the expressions should be evaluated.
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 5.2µs 8.11µs
#> 2 evens <- x%%2 == 0 32.3µs 33.38µs
#> 3 y <- x[evens] 7.4µs 8.11µs
#> 4 length(y) 1.1µs 1.67µs
#> 5 length(which(evens)) 6.9µs 7.87µs
#> 6 sum(evens) 2.6µs 3.58µ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 4.1µs 6.44µs
#> 2 evens <- x%%2 == 0 27.1µs 28.13µs
#> 3 y <- x[evens] 7.4µs 8.35µs
#> 4 length(y) 1.1µs 1.91µs
#> 5 length(which(evens)) 6.5µs 7.39µs
#> 6 sum(evens) 2.3µs 3.34µs