with_timer: Wrap a function to measure and display execution time

View source: R/with_timer.R

with_timerR Documentation

Wrap a function to measure and display execution time

Description

Wraps a function with CLI-based timing and prints its runtime in seconds. Useful for benchmarking or logging time-consuming tasks.

Usage

with_timer(fn, name = "Task")

Arguments

fn

A function to be wrapped.

name

A short descriptive name of the task (used in log output).

Details

Requires the tictoc package (CLI messages are emitted via cli).

Value

A function that executes fn(...) and prints timing information (returns invisibly).

Examples

slow_fn <- function(n) { Sys.sleep(0.01); n^2 }
timed_fn <- with_timer(slow_fn, name = "Square Task")
timed_fn(5)

evanverse documentation built on March 10, 2026, 5:07 p.m.