getTimeLimit?
On Tue, Jan 3, 2017 at 2:43 PM, William Dunlap via R-help
<r-help at r-project.org> wrote:
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?
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'.
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.
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
Bill Dunlap
TIBCO Software
wdunlap tibco.com
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.