Learn R Programming

evanverse (version 0.3.7)

with_timer: 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")

Value

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

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).

Examples

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

Run the code above in your browser using DataLab