You are currently viewing Running Time of R Code & Function

We are often required to measure the running time of an R code or timing of a function. You can get the timing in R using several methods. For example, use sys.time() to get the R code duration in seconds and use System.time() to get the timing of an R function. Besides these, there are several other functions as well which I will be covering in this R programming article.

One way to check the performance of the R code or a function is by measuring the duration it takes to execute, you can calculate this by getting the start time before executing the block and subtracting it from the time taken after the block.

1. Measure the Running Time of R Code

The timing of the R code can be measured by calculating the difference between the time at the start and at the end of the code block. Usually, the timing difference between the start and end will be returned in seconds. By using Sys.time() we can measure the running time of R code.

1.1 Sys.time() Syntax & Usage

Following is the syntax of the function.


# Syntax & Usage
start <- Sys.time()
# Add here R code or 
# Add function call
print( Sys.time() - start )

1.2 Timing in R Example

Sys.time() returns the current time in R, let’s use this to get the duration of the R code. And this approach can also be used to get the measurement of the function call.


# Get time of code execution
start <- Sys.time()
Sys.sleep(3)
print( Sys.time() - start )

Yields below output.

r time code

Similarly, you can also use this to calculate the duration of the function call.


# Get duration of function call
do_something_func <- function() { Sys.sleep(5) } 
start = Sys.time()
do_something_func()
print( Sys.time() - start )

Yields below output.

timing in r

2. Timing in R with Function

The System.time() function is used to measure the time taken to complete the function call (timing in R). This function takes the function you need to measure and prints the user, system, and elapsed fields to the console. elapsed field holds the measurement of the function call.

2.1 System.time() Syntax & Usage

Following is the syntax of the System.time() and usage to get the duration. Here, the r-expression can be a function or a code block.


# Syntax of System.time()
system.time({r-expression})

2.2 Example to get Time of R Function

Let’s run the example to measure the duration of the function call.


# Function time using system.time()
do_something_func <- function() { Sys.sleep(5) } 
system.time({do_something_func()})

Yields below output

Function execution

3. Timing in R using proc.time()

proc.time() prints a named vector of length 3. First, the total user, second the system CPU time of the current R process, and third child processes on which it has waited. The third entry represents the time in seconds since the process started.


# Get duration of code execution
start <- proc.time()
Sys.sleep(3)
print( proc.time() - start )

# Output
   user  system elapsed 
  0.008   0.008   3.004 

4. Using tictoc Library

The tictoc library contains two methods first tic() to specify the start of the measurement and toc() to specify the end of the measurement and toc() also displays the elapsed time in R.

In order to use tictoc library, you have to install R package first using install.packages('tictoc') and load it using library(tictoc).


# Get duration of function using tictoc library
library(tictoc)
do_something_func <- function() { Sys.sleep(5) } 
tic()
do_something_func()
toc()

# Output
5.024 sec elapsed

Here, the tic() function starts the timer, and the toc() function ends the timer. The elapsed output in seconds is the duration taken to execute R code that exists between these two functions.

5. Using microbenchmark Library

The microbenchmark is another library to get timing in R. This function provides the results in milliseconds along with several values as shown in the below example.

In order to use microbenchmark library, you have to install R package first using install.packages('microbenchmark') and load it using library(microbenchmark).

#Syntax of microbenchmark microbenchmark( r_expression )

Here, the r-expression can be a function or a code block. With microbenchmark() you can also call multiple functions and measure different values for each function.


# Using microbenchmark
library(microbenchmark)
do_something_func <- function() { Sys.sleep(0.3) }
microbenchmark(a1=do_something_func(), a2=do_something_func())

Yields below output.

time in r

6. Using rbenchmark Library

The benchmark() from rbenchmark library is a wrapper of System.time(). In order to use rbenchmark library, you have to install R package first using install.packages('rbenchmark') and load it using library(rbenchmark).


# Syntax of benchmark()
benchmark(do_something_func())

Below is an example of how to use benchmark() function from rbenchmark library.


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

Yields below output.

# 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

7. Complete Example


# Get running time of code execution
start = Sys.time()
Sys.sleep(3)
print( Sys.time() - start )

# Get time of function execution
do_something_func <- function() { Sys.sleep(5) } 
start = Sys.time()
do_something_func()
print( Sys.time() - start )

# Function time using system.time()
do_something_func <- function() { Sys.sleep(5) } 
system.time({do_something_func()})

# Get duration of code execution
start = proc.time()
Sys.sleep(3)
print( proc.time() - start )

# Using tictoc
library(tictoc)
do_something_func <- function() { Sys.sleep(5) } 
tic()
do_something_func()
toc()

# Using microbenchmark
library(microbenchmark)
do_something_func <- function() { Sys.sleep(0.3) }
microbenchmark(a1=do_something_func(), a2=do_something_func())

# Using rbenchmark
library(rbenchmark)
do_something_func <- function() { Sys.sleep(0.5) }
benchmark(do_something_func())

Conclusion

In this article you have learned different ways to get the execution or running time in R and timing function by using several options like System.time(), Sys.time(), proc.time(), tictoc library, microbenchmark library, and rbenchmark library.

You can find the complete example at GitHub Project

Related Articles

References