# Measure Function Execution Time in R

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 `system.time()` 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.

```# Addition function
result <- x + y
return(result)
}

start_time <- Sys.time()

# Your function or expression here
# Example usage:
num1 <- 5
num2 <- 10
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 `system.time()` to measure that addition function execution time using the above R code.

## Using the microbenchmark package

The `microbenchmark` 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.

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
result <- x + y
return(result)
}

# Multiplication function
multiplication <- function(x, y) {
result <- x * y
return(result)
}

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

result <- microbenchmark(
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.

Also Read:  Understand Set implementation in Python with Example

Below is the R code to measure multiple function execution time using `rbenchmark` package.

```# Addition function
result <- x + y
return(result)
}

# Multiplication function
multiplication <- function(x, y) {
result <- x * y
return(result)
}

install.packages("rbenchmark")
library(rbenchmark)

result <- benchmark(
multiplication(5, 10),
replications = 100  # Number of iterations for each function
)

print(result)
```

## Using the profvis package

The `profvis` 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.

The only difference in the below code is that you no need to give any commas inside `profvis` 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.

```# Addition function
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({
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 `tictoc` package offers a simple and convenient way to measure the execution time of code using `tic()` and `toc()` functions.

You need to pass your function execution inside `tic()` function and then `toc()` function will return the execution time, below is the example R code to do that.

```# Addition function
result <- x + y
return(result)
}

install.packages("tictoc")
library(tictoc)

# Measuring execution time for the addition function
tic()
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.

Also Read:  How to embed a PDF in HTML

Whether you choose the built-in `system.time()`, `microbenchmark`, `rbenchmark`, `profvis`, or `tictoc`, remember to run multiple tests for reliable and accurate results.

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.