Measuring Running Time of R Code

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

ListenData Logo
Spread the Word!
Related Posts
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

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
Looks like you are using an ad blocker!

To continue reading you need to turnoff adblocker and refresh the page. We rely on advertising to help fund our site. Please whitelist us if you enjoy our content.