Skip to content
Back to formatted view

Raw Message

Message-ID: <CAF8bMca5J+Jv52O+iUXEfMQPRm31npx1t3q6q_3DarLk=t3j+Q@mail.gmail.com>
Date: 2017-01-03T22:43:50Z
From: William Dunlap
Subject: getTimeLimit?

I am interested in measuring the time it takes to run an expression.
 system.time(expr) does this but I would like to have it report just 'more
than xxx seconds', where xxx is a argument to the timing function, when it
takes a long time.  This is useful automating the process of seeing how
processing time grows with the size of a dataset.

My latest attempt is the following function, which adds a 'censored=TRUE'
attribute when the cpu or elapsed time exceeds some limit:

system.time2 <- function (expr, gcFirst = TRUE, cpu = Inf, elapsed = Inf)
{
    setTimeLimit(cpu = cpu, elapsed = elapsed, transient = TRUE)
    censored <- NULL
    time <- system.time(gcFirst = gcFirst, tryCatch(expr, error =
function(e) if (grepl("reached (CPU|elapsed) time limit",
        conditionMessage(e)))
        censored <<- conditionMessage(e)
    else stop(e)))
    attr(time, "censored") <- censored
    time
}

It would be used as

> system.time(times <- lapply(10^(1:7), function(n)system.time2(for(i in
1:n)lgamma(1:i), elapsed=10) ))
   user  system elapsed
  33.55    0.25   33.82
> vapply(times, function(t)t[["elapsed"]], 0)
[1]  0.02  0.00  0.03  3.08 10.02 10.14 10.18
> # following gives which times are valid
> vapply(times, function(t)is.null(attr(t,"censored")), NA)
[1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE

I have two questions.
* Is this a reasonable way to compute such a censored time?
* Is there a getTimeLimit()-like function?

Also, I think it would be nice if the error thrown when timing out had a
special class so I didn't have to rely on grepping the error message, but
that is true of lots of errors.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

	[[alternative HTML version deleted]]