Stopwatch function in R

前端 未结 10 1995
后悔当初
后悔当初 2020-12-08 07:27

Is there an R timer or stopwatch function similar to MATLAB\'s tic/toc?

10条回答
  •  甜味超标
    2020-12-08 08:01

    There are plenty of profiling tools in R, as Dirk mentioned. If you want the simplicity of tic/toc, then you can do it in R too.

    EDIT: I've cannibalised the garbage collection functionality from the MATLAB package, and tic now lets you choose whether you are interested in total elapsed time or just the user time.

    tic <- function(gcFirst = TRUE, type=c("elapsed", "user.self", "sys.self"))
    {
       type <- match.arg(type)
       assign(".type", type, envir=baseenv())
       if(gcFirst) gc(FALSE)
       tic <- proc.time()[type]         
       assign(".tic", tic, envir=baseenv())
       invisible(tic)
    }
    
    toc <- function()
    {
       type <- get(".type", envir=baseenv())
       toc <- proc.time()[type]
       tic <- get(".tic", envir=baseenv())
       print(toc - tic)
       invisible(toc)
    }
    

    Usage is, e.g., tic(); invisible(qr(matrix(runif(1e6), nrow=1e3))); toc()

提交回复
热议问题