Skip to content

getTimeLimit?

2 messages · William Dunlap, Henrik Bengtsson

#
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
1:n)lgamma(1:i), elapsed=10) ))
   user  system elapsed
  33.55    0.25   33.82
[1]  0.02  0.00  0.03  3.08 10.02 10.14 10.18
[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
#
On Tue, Jan 3, 2017 at 2:43 PM, William Dunlap via R-help
<r-help at r-project.org> wrote:
I also wanted such a function, but I don't think it exists.
Internally in R the timeout is set in global variables 'cpuLimitValue'
and 'elapsedLimitValue'.  Grepping the source for it doesn't reveal
any external access to it, e.g.

$ grep -F "cpuLimitValue" -r --include="*.h" src/
src/include/Defn.h:extern0 double cpuLimitValue INI_as(-1.0);

$ grep -F "cpuLimitValue" -r --include="*.c" src/
src/main/sysutils.c:    cpuLimit = (cpuLimitValue > 0) ? data[0] +
data[1] + cpuLimitValue : -1.0;
src/main/sysutils.c:    cpuLimit = (cpuLimitValue > 0) ? data[0] +
data[1] + data[3] + data[4] + cpuLimitValue : -1.0;
src/main/sysutils.c:    double cpu, elapsed, old_cpu = cpuLimitValue,
src/main/sysutils.c:    if (R_FINITE(cpu) && cpu > 0) cpuLimitValue =
cpu; else cpuLimitValue = -1;
src/main/sysutils.c: cpuLimitValue = old_cpu;

Similar for 'elapsedLimitValue'.
FYI, R.utils::withTimeout() greps the error message (for any language;
https://github.com/HenrikBengtsson/R.utils/blob/2.5.0/R/withTimeout.R#L113-L114)
this way and returns an error of class TimeoutException.

FYI 2, there is as 'Working group for standard error (condition)
classes' proposal to the RConsortium "wishlist", cf.
https://github.com/RConsortium/wishlist/issues/6.

/Henrik