Open In App

How to Measure Execution Time of Function in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to measure the execution or running time of a function in the R programming language. 

Method 1: Using Sys.time

For this first create a sample function that runs for a specific duration. For doing so pass the duration to Sys.sleep() function.

Syntax:   

startTime <- Sys.time()

func()

endTime <- Sys.time()

Store/record the time before the execution in the variable startTime, then after the execution of the function, store the time in endTime variable. To get the system’s current time we have used Sys.time() function, it does not accept any parameters.

Then we calculate the difference between the time after execution and the time before execution, this gives us the time of execution(running time) of the function.

Example:

R




# sample func, whose exec time will be measured
sleep_func <- function() { Sys.sleep(5) } 
startTime <- Sys.time()
  
sleep_func()
endTime <- Sys.time()
  
# prints recorded time
print(endTime - startTime)


Output:

Time difference of 5.005118 secs

Method 2: using system.time()

First, create a sample function that runs for a specific duration. Then call sleep_func from within the parameter of system.time( {} ), this measures and returns the execution time of our sleep_func().

system.time({}) is a simple function that takes any R expression or code or function as an argument and returns its execution time.

Note: That the R expression must be enclosed in “{ } parenthesis before passing it as an argument.

Syntax: system.time({ R-expression })

Parameters: 

R-expression: Any R expression whose execution time is to be measured.

Example:

R




# sample func, whose exec time will be measured
sleep_func <- function() { Sys.sleep(5) } 
  
system.time({sleep_func()})


Output:

   user  system elapsed 

  0.000   0.000   5.005 

Method 3: using tictoc library

We have to first install and import the library called “tictoc“. Then we define our sample function that runs for some specific duration. Call the tic() function, then place any R expression or code or function(), then end it with toc() function call. It will print the execution time of the sleep_func(). The tic() function starts the timer and toc() function ends the timer, elapsed time gets measured for whatever code is placed between these function calls.

Example:

R




library(tictoc)
  
sleep_func <- function() { Sys.sleep(5) }
  
tic()
sleep_func()
toc()


Output:

5.025 sec elapsed

Method 4: using microbenchmark library

We have to first install and import the library called “microbenchmark” and create a sample function to work for a specific duration.

Syntax: microbenchmark( func() )

Parameters: Any R expression or function

Pass sleep_func() in as an argument in the microbenchmark() function. It will print the benchmark scores of the sleep_func() function. It first prints the unit being used, then prints all the details like minimum time, maximum time, mean, median, etc. In our example Unit is milliseconds.

microbenchmark() function in R programming language provides the infrastructure for accurate measurements and comparisons of the execution time of R expressions. This function often works as a replacement for system.time() method which we saw in Method 2 as it provides more accurate results.

Example:

R




library(microbenchmark)
  
sleep_func <- function() { Sys.sleep(0.5) }
  
microbenchmark(sleep_func())


Output:

Unit: milliseconds

         expr      min       lq     mean   median       uq      max neval

 sleep_func() 500.1297 500.5133 500.9525 500.9391 501.0605 506.7048   100

Method 5: using rbenchmark library

First, install and import the library called “rbenchmark”. 

Syntax: benchmark( func() )

Parameters: Any R expression or function

Pass sleep_func() in as an argument in the benchmark() function. It will print the benchmark scores of the sleep_func() function.

Example:

R




library(rbenchmark)
  
sleep_func <- function() { Sys.sleep(0.5) }
  
benchmark(sleep_func())


Output:

          test replications elapsed relative user.self sys.self user.child sys.child

1 sleep_func()          100   50.08        1      0.02        0         NA        NA



Last Updated : 17 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads