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"(attachattr(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. Default0.
See also
Other decorators:
cache_clear(),
decorate(),
fmisc_decorators,
is_decorated(),
print.fmisc_decorated(),
undecorate(),
with_cache(),
with_logging(),
with_rate_limit(),
with_retry()
Examples
# \donttest{
g <- with_timing(function(x) {
Sys.sleep(0.05)
x
}, .threshold = 0)
g(42)
#> ℹ function(x) { took 0.050s
#> [1] 42
# }