As a data scientist or analyst, it’s crucial to make our code run faster and more efficiently when working with big data or complex algorithms in R. To achieve this, measuring the time taken by a function to execute is essential. In this tutorial, we will explore different methods to measure the execution time of functions in R, helping you optimize your code and make it more efficient.

## Using the system.time() function

R comes with a built-in function called

that helps us measure the time taken to execute a particular function or task. It gives us information about the total time, user time, system time, and elapsed time.**system.time()**

```
# Addition function
addition <- function(x, y) {
result <- x + y
return(result)
}
start_time <- Sys.time()
# Your function or expression here
# Example usage:
num1 <- 5
num2 <- 10
sum_result <- addition(num1, num2)
print(sum_result)
end_time <- Sys.time()
execution_time <- end_time - start_time
print(execution_time)
```

```
> print(execution_time)
Time difference of 0.002778053 secs
```

Just for this example, I used a function to add two numbers. Then I used

to measure that addition function execution time using the above R code.**system.time()**

## Using the microbenchmark package

The

package is a powerful tool to measure the execution time of functions, especially when comparing multiple functions. It provides more accurate results by running the function multiple times, considering any variations in execution time.**microbenchmark**

First, you need to install the package if you haven’t already:

```
install.packages("microbenchmark")
library(microbenchmark)
```

Next, measure the execution time of your function. **Note:** Here in this example, I used two functions addition and multiplication.

```
# Addition function
addition <- function(x, y) {
result <- x + y
return(result)
}
# Multiplication function
multiplication <- function(x, y) {
result <- x * y
return(result)
}
install.packages("microbenchmark")
library(microbenchmark)
result <- microbenchmark(
addition(5, 10),
multiplication(5, 10),
times = 100 # Number of iterations for each function
)
print(result)
```

## Using the rbenchmark package

The `rbenchmark`

package is another option for measuring function execution time in R. It allows us to compare the performance of multiple functions easily.

Below is the R code to measure multiple function execution time using

package.**rbenchmark**

```
# Addition function
addition <- function(x, y) {
result <- x + y
return(result)
}
# Multiplication function
multiplication <- function(x, y) {
result <- x * y
return(result)
}
install.packages("rbenchmark")
library(rbenchmark)
result <- benchmark(
addition(5, 10),
multiplication(5, 10),
replications = 100 # Number of iterations for each function
)
print(result)
```

## Using the profvis package

The

package is an excellent tool for profiling R code to find performance issues. It visualizes the function execution time, making it easier to identify areas that need optimization.**profvis**

The only difference in the below code is that you no need to give any commas inside

function. Below code will generate a profile tab in your R studio. In this profile tab you can find two pieces of information: Flam Graph and Data.**profvis**

```
# Addition function
addition <- function(x, y) {
result <- x + y
return(result)
}
# Multiplication function
multiplication <- function(x, y) {
result <- x * y
return(result)
}
# Install profvis package in R
install.packages("profvis")
library(profvis)
result <- profvis({
addition(5, 10)
multiplication(5, 10)
replications = 100 # Number of iterations for each function
})
print(result)
```

## Using the tictoc package

This is not the tictoc you are thinking off. This tictoc is a R library. The

package offers a simple and convenient way to measure the execution time of code using **tictoc**

and **tic()**

functions.**toc()**

You need to pass your function execution inside

function and then **tic()**

function will return the execution time, below is the example R code to do that.**toc()**

```
# Addition function
addition <- function(x, y) {
result <- x + y
return(result)
}
# Load the tictoc package
install.packages("tictoc")
library(tictoc)
# Measuring execution time for the addition function
tic()
result_addition <- addition(10, 20)
toc()
```

```
> toc()
0 sec elapsed
```

## Conclusion

In this tutorial, we explored different methods to measure function execution time in R. By using these techniques, you can identify performance issues in your code and optimize it for better efficiency.

Whether you choose the built-in

, **system.time()**

, **microbenchmark**

, **rbenchmark**

, or **profvis**

, remember to run multiple tests for reliable and accurate results. **tictoc**

Improving the execution time of your functions can lead to significant improvements in your data analysis and data science projects. This is it for this tutorial. If you have any questions or suggestions regarding this tutorial, please let me know in the comment section below.

Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.