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 parametermtry <- 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 Forestrf <-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
I have a doubt. I'm writing R code as a function statement. for example:
ReplyDeletefile_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.
Check out this code -
Deletefile_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")