How to Use Sys.time() to Time Code in R


Often you may want to time how long it takes for various code blocks to run in R.

This is particularly useful to do when you want to compare two different techniques for performing some task and you want to determine which technique is fastest.

One of the easiest ways to do so in R is by using the Sys.time() function from Base R, which returns the current date and time on your computer.

You can use the following basic syntax with the Sys.time() function to time how long it takes R to execute any block of code:

#calculate time at start of code block
start_time <- Sys.time()

#SOME CODE

#calculate time at end of code block 
end_time <- Sys.time()

#calculate difference between start and end time
total_time <- end_time - start_time

#display total time
total_time

Here is what this syntax does:

  • First, we get the current date and time and store it in a variable named start_time
  • Next, we execute a block of code
  • Next, we get the current date and time as soon as the code is finished running and store it in a variable named end_time
  • Lastly, we calculate the difference between the start and end time and store it in a variable named total_time

The total_time variable then tells us how long it took for the particular block of code to run in R.

The following example shows how to use this syntax in practice.

Example: How to Use Sys.time() to Time Code in R

Suppose that we create the following data frame in R that contains information about points scored by basketball players on two different teams:

#make this example reproducible
set.seed(1)

#create data frame
df <- data.frame(team=rep(c('A', 'B'), each=500),
                 points=rnorm(1000, mean=20))

#view data frame
head(df)

  team   points
1    A 19.37355
2    A 20.18364
3    A 19.16437
4    A 21.59528
5    A 20.32951
6    A 19.17953

Suppose that we would like to calculate the mean points scored by players on each team using two different techniques:

Technique 1: Use functions from base R

Technique 2: Use functions from the dplyr Package

Suppose that we would like to time how long it takes for each technique so that we can determine which approach is fastest.

First, we can use the Sys.time() function to time how long it takes to perform this task using base R:

#calculate time at start of code block
start_time <- Sys.time()

#calculate mean points value grouped by team
aggregate(df$points, list(df$team), FUN=mean)

#calculate time at end of code block 
end_time <- Sys.time()

#calculate difference between start and end time
total_time <- end_time - start_time

#display total time
total_time

  Group.1        x
1       A 20.02264
2       B 19.95406

Time difference of 0.005294323 secs

From the output we can see that this task took .00529 seconds to run using this base R approach.

Next, we can use the Sys.time() function to time how long it takes to perform this task using dplyr:

library(dplyr)

#calculate time at start of code block
start_time <- Sys.time()

#calculate mean points value grouped by team
df %>% group_by(team) %>% summarise_at(vars(points), list(name = mean))

#calculate time at end of code block 
end_time <- Sys.time()

#calculate difference between start and end time
total_time <- end_time - start_time

#display total time
total_time

  Group.1        x
1       A 20.02264
2       B 19.95406

Time difference of 0.2363865 secs

From the output we can see that this task took .23638 seconds to run using this dplyr approach.

For this particular example, the base R approach is quicker.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Read Zip Files in R
How to Import CSV Files into R
How to Import Excel Files into R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *