Is there an R timer or stopwatch function similar to MATLAB\'s tic/toc?
Just for completeness: you can actually 'simulate' tic and toc in R, so that you can write
tic
## do something
toc
without parentheses. The trick is to abuse the print
function, as demonstrated in Fun: tic and toc in R:
tic <- 1
class(tic) <- "tic"
toc <- 1
class(toc) <- "toc"
print.tic <- function(x, ...) {
if (!exists("proc.time"))
stop("cannot measure time")
gc(FALSE)
assign(".temp.tictime", proc.time(), envir = .GlobalEnv)
}
print.toc <- function(x,...) {
if (!exists(".temp.tictime", envir = .GlobalEnv))
stop("did you tic?")
time <- get(".temp.tictime", envir = .GlobalEnv)
rm(".temp.tictime", envir = .GlobalEnv)
print(res <- structure(proc.time() - time,
class = "proc_time"), ...)
invisible(res)
}
So typing
tic
Sys.sleep(2)
toc
should results in something like this:
user system elapsed
0.000 0.000 2.002
As I said, it's a trick; system.time, Rprof and
packages such as rbenchmark are the way to measure
computing time in R.