Skip to contents

Wraps f so each call is timed with proc.time(). Reports elapsed time on both success and failure (via on.exit()), either as a cli message, attached as an "elapsed" attribute on the return value, or passed to a user callback.

Usage

with_timing(
  f,
  ...,
  .report = c("message", "attribute", "callback"),
  .callback = NULL,
  .threshold = 0
)

Arguments

f

A function to wrap.

...

Reserved for future extension.

.report

One of "message" (default; cli info alert), "attribute" (attach attr(result, "elapsed")), "callback" (invoke .callback(name, elapsed, ok)).

.callback

Function used when .report = "callback".

.threshold

Non-negative numeric. Only report when elapsed time in seconds is >= .threshold. Default 0.

Value

A function that forwards its arguments to f.

Examples

# \donttest{
g <- with_timing(function(x) {
  Sys.sleep(0.05)
  x
}, .threshold = 0)
g(42)
#> ℹ function(x) { took 0.050s
#> [1] 42
# }