Measuring Running Time of R Code


R Data Science: R Programming A-Z: R For Data Science With Real Exercises!

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)
time.taken

Result : Time difference of 30.15 secs

Coursera Data Science

R Tutorials : 75 Free R Tutorials

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)
    return(df)
    }

    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.

    ReplyDelete
    Replies
    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))
      return(df)
      }


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

      Delete

Next → ← Prev