Timing R code with Sys.time()
I can run a piece of code for 5 or 10 seconds using the following code: period <- 10 ## minimum time (in seconds) that the loop should run for tm <- Sys.time() ## starting data & time while(Sys.time() - tm < period) print(Sys.time()) The code runs just fine for 5 or 10 seconds. But when I replace the period value by 60 for it to run for a minute, the code never stops. What is wrong? As soon as elapsed time exceeds 1 minute, the default unit changes from seconds to minutes. So you want to control the unit: while (difftime(Sys.time(), tm, units = "secs")[[1]] < period) From ?difftime If ‘units =