Measuring function execution time in R

In R, you can measure the execution time of a function using the system.time() function. Here is an example:

f <- function() {
# code to be executed
result <- system.time(f())

The system.time() function returns an object of class “proc_time”, which contains information about the elapsed time, user time, and system time. The elapsed time is the total wall-clock time for the function to run, the user time is the time spent executing the function in user mode, and the system time is the time spent executing the function in system mode.

You can access the elapsed time using the elapsed component of the proc_time object:

elapsed_time <- result$elapsed

Keep in mind that the system.time() function measures the time required for a single execution of a function, so if you want to measure the average execution time over multiple runs, you will need to call the function multiple times and compute the average.