Measuring Running Time of R Code

Live Online Training : Data Science with R

- Explain Advanced Algorithms in Simple English
- Live Projects
- Case Studies
- Job Placement Assistance
- Get 10% off till Oct 26, 2017
- Batch starts from October 28, 2017

To measure execution time of R code, we can use Sys.time function. Put it before and after the code and take difference of it to get the execution time of code.
start.time <- Sys.time()

#Selecting Optimum MTRY parameter
mtry <- tuneRF(dev[, -1], dev[,1], ntreeTry=500, stepFactor=1.5,improve=0.05, trace=TRUE, plot=TRUE)
best.m <- mtry[mtry[, 2] == min(mtry[, 2]), 1]

#Train Random Forest
rf <-randomForest(classe~.,data=dat3, mtry=best.m, importance=TRUE,ntree=50)

end.time <- Sys.time()
time.taken <- round(end.time - start.time,2)

Result : Time difference of 30.15 secs

R Tutorials : 75 Free R Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

2 Responses to "Measuring Running Time of R Code"

  1. I have a doubt. I'm writing R code as a function statement. for example:

    file_read<- function(input_file){
    df = read.csv(input_file)

    I need to measure the complation time with the date. can i use your start.time and end.time within your function, if so, can you please explain?. or any other ways to do that.

    1. Check out this code -
      file_read<- function(input_file){
      start.time <- Sys.time()
      df = read.csv(input_file)
      end.time <- Sys.time()
      print(round(end.time - start.time,2))

      mydata = file_read("C:\\Users\\Deepanshu\\Downloads\\dataset.csv")


Next → ← Prev