How does setTimeLimit work in R?

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:

  • How does it monitor the elapsed time? Ie 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.

  • 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 (ie 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.

  • 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 .)

  • 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?


  • 为了显示在C函数调用期间setTimeLimit不起作用:

    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))
    
    链接地址: http://www.djcxy.com/p/9870.html

    上一篇: 确定真正的调用程序集的方法

    下一篇: setTimeLimit如何在R中工作?