In this tutorial you’ll learn how to measure the time of a process within R. You would want to do this in order to benchmark different process or if you simply want to know how much time training that random forest with a data set of 500k rows took.
# One easy way for stopping the time is the built in function system.time
# With this easy, but somewhat messy way we simply wrap system.time ({}) around the process in question.
# It's advisable using this only for short chunks of code. But for sake of example, let's use this
# function that calculates the prime numbers in the range 1:10000:
system.time({
my_prime_numbers <- c(2)
for(i in seq(3,10000,2)){
is_prime = TRUE
for( j in my_prime_numbers){
if((i %% j) ==0){
is_prime = FALSE
break
}
}
if(is_prime){
my_prime_numbers[length(my_prime_numbers)+1] <- i
}
}
})
## user system elapsed
## 0.150 0.009 0.158
# Another way is Sys.time (also built in)
# Here you simply calculate the run time of a process by taking the difference between the start and end of the process you are running:
start_time <- Sys.time()
my_prime_numbers <- c(2)
for(i in seq(3,10000,2)){
is_prime = TRUE
for( j in my_prime_numbers){
if((i %% j) ==0){
is_prime = FALSE
break
}
}
if(is_prime){
my_prime_numbers[length(my_prime_numbers)+1] <- i
}
}
run_time <- Sys.time() - start_time
run_time
## Time difference of 0.1395249 secs
# The third option is using the package tictoc. It works similar to Sys.time above, but is a tad more
# convenient
library(tictoc)
tic("prime_numbers")
my_prime_numbers <- c(2)
for(i in seq(3,10000,2)){
is_prime = TRUE
for( j in my_prime_numbers){
if((i %% j) ==0){
is_prime = FALSE
break
}
}
if(is_prime){
my_prime_numbers[length(my_prime_numbers) + 1] <- i
}
}
toc()
## prime_numbers: 0.145 sec elapsed
In this tutorial we learnt not just one but three methods for measuring the run time of processes in R.