How to Measure Execution Time in R (5 Methods + Examples)

Measuring the execution time is common in programming to get a better idea of how a program or a part of the code is performing.

This is a comprehensive guide to measuring the elapsed time in R. In this guide, there are some built-in mechanisms and examples of popular libraries’ ways of computing the execution times.

Here’s a short overview of the approaches you will learn in this guide:

  1. Sys.time()
  2. system.time()
  3. Tictoc library
  4. Microbenchmark Library
  5. Rbenchmark Library

Let’s take a closer look at these methods and see some examples.

Method 1: Use Sys.time()

An easy way to measure the execution time of a piece of code is by using the built-in Sys.time() function.

To measure the execution time with the Sys.time() function:

  1. Call Sys.time() before the part of code you want to measure and store the time instance into a variable.
  2. Run the piece of code whose execution time you want to measure.
  3. Call Sys.time() again and store the time instance to a variable.
  4. Subtract the last Sys.time() call from the first Sys.time() call. This returns a description of the time difference between these two instances of time.

Here’s an example code where I measure the time of a function called example_func:

example_func <- function() { Sys.sleep(5) }

start <- Sys.time()
example_func()
end <- Sys.time()

print(end - start)

Output:

Time difference of 5.000719 secs

Method 2: Use system.time()

Another way to measure the execution time in R is by using the system.time() function. Unlike the Sys.time() function, the system.time() function takes an expression, evaluates it, and measures the elapsed time.

Here’s an example of measuring how long it takes to run an example function:

example_func <- function() { Sys.sleep(5) }

system.time({example_func()})

Output:

   user  system elapsed 
  0.001   0.000   5.018 

But what do these values mean? To put it short, the elapsed column reveals the time it took to run your code.

Here’s a more specific breakdown of the returned values:

  1. user. This is the user CPU time spent by the current R session.
  2. system. This is the system CPU time that the operating system spent on behalf of the current process.
  3. elapsed. The execution time of the actual code you are measuring.

Method 3: Use tictoc Library

Thus far, you’ve seen examples of measuring the execution time with built-in mechanisms. Sometimes using an external library to measure elapsed time can be handy as it makes code readable and intuitive.

One example of such a library is the tictoc library.

Notice that you need to install the tictoc library first before you can use it.

The idea of the tictoc function is to make measuring the elapsed time easy and readable:

  1. Call tic() to start the clock.
  2. Run some code.
  3. Call toc() to stop the clock.

Calling toc() shows the elapsed time in the console.

Here’s an example of measuring the execution time of a sample function in R:

library(tictoc)

example_func <- function() { Sys.sleep(3.5) }

tic()
example_func()
toc()

Output:

3.525 sec elapsed

Method 4: Use microbenchmark Library

Another library you might want to consider to measure the execution time of your program is the microbenchmark library.

To then use this library to measure the elapsed time, you need to run code inside the microbenchmark() function call.

This function not only returns the time it took to execute your code but also some useful details, such as minimum time, maximum time, median elapsed time, and such based on the numerous runs the function performs.

For example:

library(microbenchmark)

example_func <- function() { Sys.sleep(0.1) }

microbenchmark(example_func())

Output:

Unit: milliseconds
           expr      min       lq     mean   median       uq      max neval
 example_func() 100.1813 100.4506 101.3933 100.5973 100.8105 142.5669   100

Let’s take a quick look at the returned values:

  • expr is the code expression you want to find the execution time for.
  • min is the minimum amount of time it took to run the code out of the numerous tries.
  • lq is the lower quartile, that is, 25% of the runs took at most this amount of time to run.
  • mean is the average execution time.
  • median is the median time it took to run the code.
  • uq is the upper quartile, that is, 75% of the runs took at most this amount of time to run.
  • max is the maximum amount of time that the code ran.
  • neval is the number of evaluations which is 100 by default.

To change the number of times the code is timed, specify the optional times parameter in the microbenchmark() function call.

For example, let’s only do 10 runs:

library(microbenchmark)

example_func <- function() { Sys.sleep(0.1) }

microbenchmark(example_func(), times=10)

Output:

Unit: milliseconds
           expr      min       lq     mean   median       uq    max neval
 example_func() 100.3563 100.6037 103.1201 100.6237 100.7375 125.16    10

Using the microbenchmark library offers a great replacement for the system.time() function thanks to the level of detail it gives you about the code execution.

Method 5: Use rbenchmark Library

rbenchmark is another useful timing library for measuring execution times. rbenchmark is a great alternative to the system.time() function is the rbenchmark library.

Once again, you need to install this library before using it!

Very similar to the previous examples, the rbenchmark library comes with a built-in timing function, benchmark(). This function takes the piece of code you want to time as an argument. It then returns a bunch of data related to the runs it performed.

Notice that this method returns the total amount of time elapsed. By default, the function runs your code 100 times. So for example, if your code takes 1 second to run, this method returns an elapsed time of 100 seconds because it runs the code 100 times.

For example, let’s measure the execution time of an example function.

library(rbenchmark)

example_func <- function() { Sys.sleep(0.2) }

benchmark(example_func())

Output:

            test replications elapsed relative user.self sys.self
1 example_func()          100  20.183        1       0.1        0

As another example, let’s change the number of runs to 10 from the 100 default:

library(rbenchmark)

example_func <- function() { Sys.sleep(0.2) }

benchmark(example_func(), replications=10)

Output:

            test replications elapsed relative user.self sys.self
1 example_func()           10   2.036        1      0.02        0

Summary

Today you learned how to measure the execution time of your program in R.

To recap, you can use basic built-in mechanisms as well as some advanced timers by using R libraries.

A simple and native way to measure elapsed time is by using the system.time() function or the Sys.time() function.

If you want to make the code more clean, you use the tictoc library. It makes computing the execution time easy by using the tic() and toc() function calls.

To get more details about the elapsed time, you can use a library like rbenchmark or microbenchmark.

Thanks for reading. Happy coding!

Read Also