Search code examples
rsystemtime

Time since the code has been running


The following code shows total code run time: Dummy code;

require(tictoc)
require(svMisc)
tic()
y<-NA
for(i in 1:1000000){
  progress(i)
  x<-sample(rnorm(1000),1)
  y[i]<- sqrt(x)
}
max(y, na.rm = T)
toc()

I want to see the running time (stopwatch) only on the console without waiting for code run completion, similar to the code completion percentage display on the console using svMisc package. I can not use any profiling method based on utils or profvis package due to some weird rstudio crashing.


Solution

  • Try

    start <- Sys.time()
    for (i in 1:10) {
        elapsed <- difftime(Sys.time(), start, units = "secs")
        cat(sprintf("\r%1.0f seconds elapsed", elapsed))
        Sys.sleep(1)
    }
    cat("\n")