I want to print start time, end time and total execution in R and add a shortcut to it

old <- Sys.time()

      

// MY code

new <- Sys.time()

      

total time = old-new the output shows "Time difference -6.661923 sec"

instead I want "Runtime: 0.35 sec"

+3


source to share


2 answers


You can use sprintf

as below:

old <- Sys.time()
rnorm(500,0,1)
new <- Sys.time()
x <- (new - old)

sprintf("The execution time is %5.2f secs",x)

      



Output

[1] "The execution time is  1.08 secs"

      

+5


source


Something like



old <- Sys.time()
#code
new <- Sys.time()
total_time <- paste0("Execution time: ", as.numeric(new-old), "secs")

      

+2


source







All Articles