Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does setTimeLimit work in R?

Tags:

r

timer

I am trying to master setTimeLimit() in R and my experience has led to several related questions, so maybe the fundamental question is: how does this really work? (I have been looking at evalWithTimeout() from R.utils as well, and it may suit my purposes slightly better, but it's built on this function.)

Here are the key things I am trying to figure out:

  1. How does it monitor the elapsed time? I.e. it seems to get inserted into the flow control, so how does it do that? Being able to have "background" processes is cool, and could be used for reporting status, checkpointing, and more.

  2. Can I determine how much time remains until it is triggered? I realize I can wrap it and store, somewhere, the elapsed & CPU time consumed at about the point of invocation (i.e. the output of proc.time()). But, this function is already storing these somewhere and I'd like to know where, or at least how to determine the time remaining.

  3. Can it be made to do something useful if the R console is idle? Being able to monitor elapsed.time() and cpu.time() is very useful. I'd like to be able to monitor when R is idle, but it seems from tinkering that it requires a command to be submitted or completed. Moreover, just outputting an error doesn't trigger a subsequent action. (Maybe I need to give more attention to evalWithTimeout.)

  4. The help information says that it can be applicable with C or Fortran, but doesn't give examples. Any suggestions on how this should be done?

like image 902
Iterator Avatar asked Oct 26 '11 13:10

Iterator


1 Answers

To show that setTimeLimit does not work during a C function call:

rfunction <- function(){
    repeat{
        x <- rnorm(100);
    }
}

cfunction <- function(){
    x <- eigen(matrix(rnorm(1e6), 1e3));
}

setTimeLimit(3);
system.time(try(rfunction(), silent=TRUE))
system.time(try(cfunction(), silent=TRUE))
like image 92
Jeroen Ooms Avatar answered Nov 06 '22 06:11

Jeroen Ooms