Search code examples
rtimebenchmarking

Get time difference always in minutes


I need to save the run-time of my algorithms and after that, I'd like to have the mean value but I have a problem with the metrics. Sometimes time difference return minutes and sometimes second. How I can always obtain minutes?

This is a short example, where when I calculated the mean, there is a "bug". How I can fix it?

waitTimes <- c(50,60,70)
saveTimes <- list()
for(i in 1:length(waitTimes)){
  start <- Sys.time()

  Sys.sleep(waitTimes[i])

  end <- Sys.time()

  saveTimes[[i]] <- end - start
}
tim <- c()
for(i in 1:length(saveTimes)){
  tim <- c(tim,saveTimes[[i]])
}

Solution

  • Instead of just subtracting one time from another, use difftime so you can specify the unit as minutes:

    start <- Sys.time()
    end <- Sys.time()
    
    
    difftime(end, start, units = 'mins')
    #Time difference of 0.1102342 mins