Skip to contents

This utility wraps a function with CLI-based timing and displays its runtime in seconds. It is especially 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)

Value

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

Details

This function requires the cli and tictoc packages.

Examples

slow_fn <- function(n) { Sys.sleep(0.2); n^2 }
timed_fn <- with_timer(slow_fn, name = "Square Task")
timed_fn(5)
#> 
#> ── Square Task started at 2025-04-24 11:22:24.291581 ───────────────────────────
#>  Square Task completed in "0.210" seconds