Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the time a script has been running in R [duplicate]

Tags:

time

r

For simple, home-made benchmarking I'd like to add a timer to my R script so that I know for how long it has been running. It's a script that's unleashed on loads of data so it can take for over an hour to complete. Therefore I'm looking for a method that tells me the exact time the script has been running.

The idea that I got was:

old = getCurrentTime()

# Do the rest of my script

(new = getCurrentTime() - old)

I don't know if this makes sense, but it seems the best way to do this, without having a counter running in the background, is by comparing the start time of the script and the time and the end and print the difference. However, I'm not sure how to get the time in R, get the difference, and format it in hh:mm:ss.

like image 370
Bram Vanroy Avatar asked Dec 06 '22 20:12

Bram Vanroy


2 Answers

You can use Sys.time()

old <- Sys.time() # get start time

# some code
#...

# print elapsed time
new <- Sys.time() - old # calculate difference
print(new) # print in nice format

However, there's also the microbenchmark package for more sophisticated / accurate timing using multiple trials etc.

like image 92
arvi1000 Avatar answered Dec 25 '22 06:12

arvi1000


Your general approach was correct and is probably the most conventional way to accomplish this, regardless of the programming language. However, subtracting two POSIXt objects will give you an object of class difftime, where the unit of measurement is selected automatically (depending on the size of the difference), rather than the "HH:MM:SS" format you are looking for. Writing a function return this format is pretty straightforward, e.g. something like

hms_span <- function(start, end) {
  dsec <- as.numeric(difftime(end, start, unit = "secs"))
  hours <- floor(dsec / 3600)
  minutes <- floor((dsec - 3600 * hours) / 60)
  seconds <- dsec - 3600*hours - 60*minutes
  paste0(
    sapply(c(hours, minutes, seconds), function(x) {
      formatC(x, width = 2, format = "d", flag = "0")
    }), collapse = ":")
}

(t0 <- Sys.time() - 3600 * 8.543)
#[1] "2015-08-19 03:48:36 EDT"
(t1 <- Sys.time())
#[1] "2015-08-19 12:19:24 EDT
R> hms_span(t0, t1)
#[1] "08:32:34"
like image 42
nrussell Avatar answered Dec 25 '22 05:12

nrussell