[R] to print system.time always

jim holtman jholtman at gmail.com
Sat Nov 3 19:55:01 CET 2012


I use notepad++ on Windows, so it is easy to add a "hotkey" that will
surround a block of code that you want to execute with:

system.time({......code to run......})

Usually you don't want it around each statement.  I use the following
function to have it print out CPU and memory usage at various
locations in my script since it allows me to see the progress and
where time might be going:

"my.stats" <-
local({
    # local variables to hold the last times
    # first two variables are the elasped and CPU times from the last report

    Level    <- 1
    MaxLevel <- 30
    Stack    <- matrix(0, ncol=2, nrow=MaxLevel)
    function(text = "stats", reset=FALSE, oper="")
    {
        procTime <- proc.time()[1:3]    # get current metrics
        if (reset){ # setup to mark timing from this point
            Level <<- 1     # reset the Level
            Stack[Level, ] <<- c(procTime[3], procTime[1] + procTime[2])
        }
        if (oper == "push"){
            if (Level < MaxLevel) Level <<- Level + 1
            Stack[Level, ] <<- c(procTime[3], procTime[1] + procTime[2])
        }
        .caller <- sys.calls()
        if (length(.caller) == 1) .caller <- "Rgui"
        else .caller <- as.character(.caller[[length(.caller) - 1]])[1]
        cat(sprintf("%s (%d) - %s : %s <%.1f %.1f> %.1f : %.1fMB\n",
            text,
            Level,
            .caller,
            format(Sys.time(), format="%H:%M:%S"),
            procTime[1] + procTime[2] - Stack[Level, 2],
            procTime[3] - Stack[Level, 1],
            procTime[3],
            memory.size()))
        if ((oper == "pop") && (Level > 1)) Level <<- Level - 1
        else if (oper == "reset") Level <<- 1
        invisible(flush.console())  # force a write to the console
    }
})

It produces output like this:

> my.stats('start')
start (1) - Rgui : 14:53:16 <39.2 597822.1> 597822.1 : 1213.8MB
> system.time(for(i in 1:col) ym[, i] <- cumsum(x[,i]))
   user  system elapsed
   1.77    0.01    1.80
> my.stats('done')
done (1) - Rgui : 14:53:23 <41.0 597828.6> 597828.6 : 1213.8MB


This says that between 'start' and 'done',   1.8 CPU seconds were used
(41.0 - 39.2) which is what syste.time was reporting.




On Sat, Nov 3, 2012 at 11:52 AM, mrzung <mrzung46 at gmail.com> wrote:
> Hi all;
>
> I want to print system.time whenever I execute any command.
>
> It takes too much time to type "system.time()" function to all command.
>
> is there any solution on it?
>
> And,
>
> apply(matrix,1,cumsum) command is too slow to some large matrix.
>
> is there any function like rowCumSums ?
>
> thank u!
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/to-print-system-time-always-tp4648314.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list