In this article, we will share with you how to get or calculate the `timing in R(times in R)`

. To do that, you can use the `Sys.time()`

function or other functions which are built in the R-package. Follow us to learn more about it with the explanation and examples below.

**Timing In R(Times In R)**

**The usage of the timing in R and which functions can perform it?**

To get the timing in R, you can use the Sys.time() function, which is available in R. And the primary function which many people want to do when using the timing in R is to calculate the performance of the R code. In other words, it is used to calculate the running time of the R code. To do that, you can use the `Sys.time()`

function, the `proc.time()`

function, the `'tictoc'`

package, the `'microbenchmark'`

package, … Let’s take a look at the Sys.time() function, which is the most popular used with the timing in R.

**Syntax**

`system.time(expr, gcFirst)`

**Parameters**

**expr:**The R expression.**gcFirst:**The default is TRUE. Collect the garbage before timing or not.

We have introduced what you can do with the timing in R. We will show you how to perform it in R in the next title below.

**How To Perform The Timing In R**

**Get the current time in R**

You can get the current time in R by the Sys.time() function.

Look at the example below.

# Get the current time Sys.time()

**Output**:

`[1] "2022-12-20 12:46:50 UTC"`

This output will change at different times.

**Calculate the running time of the R code by the Sys.time()**

Besides getting the current time, the Sys.time() function can be used to calculate the performance of the R code.

Look at the example below.

# Create the beginning time start <- Sys.time() my_func <- function(x, y) { return(x * y + x + 2 * y + x * x) } my_func(1, 2) # The ending time end <- Sys.time() # Calculate the running time result <- end - start result

**Output**:

```
[1] 8
Time difference of 7.772446e-05 secs
```

Note the Sys.time() also consumes time, but it is not significant.

**Calculate the running time of the R code by the proc.time() function.**

You can calculate the running time of the R code by the proc.time() function.

Look at the example below.

# Create the beginning time start <- proc.time() Sys.sleep(1) # The ending time end <- proc.time() # Calculate the running time result <- end - start result

**Output**:

```
user system elapsed
0.000 0.000 1.002
```

‘elapsed’ is the running time of the code.

**Calculate the running time of the R code by the ‘tictoc’ package**

You can use the tic() function to store the beginning of the time and the toc() function to store the ending of the time and calculate the running time of the R code by the tic() and toc() functions in the ‘tictoc’ package.

Look at the example below.

library('tictoc') # Create the beginning time tic() Sys.sleep(1) # Calculate the running time toc()

**Output**

`1.02 sec elapsed`

**Calculate the running time of the R code by the ‘rbenchmark’ package**

You can use the benchmark() function in the ‘rbenchmark’ package to calculate the running time of the R code.

Look at the example below.

library("rbenchmark") myfunc <- function(x, y) { return(x * y + x + 2 * y + x * x) } # Calculate the running time of the 'myfunc' function benchmark(myfunc(1, 2))

**Output**:

```
test replications elapsed relative user.self sys.self user.child
1 myfunc(1, 2) 100 0.04 1 0.04 0 0
sys.child
1 0
```

‘elapsed’ is the running time of the code.

Besides, you can learn the series of R tutorials here.

**Summary**

We have shared with you how to perform the` timing in R (times in R)`

. The timing in R is widely used to get the current time or calculate the running time of the R code. To do that, you can use the `Sys.time()`

function, which is available in R, is easy to use, and you do not have to install any packages. We hope this tutorial is helpful to you. Thanks!

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!

**Name of the university:** PTIT

**Major**: IT

**Programming Languages**: C, C++, Java, Python, JavaScript, R