[R] Timer on a function

William Dunlap wdunlap at tibco.com
Fri Mar 16 00:14:03 CET 2012


There is a setTimeLimit function in base.  It could be encapsulated into
the following to limit the time spent on an expression:

timeOut <- function (expr, ...)  {
    on.exit(setTimeLimit())
    setTimeLimit(...)
    expr
}

E.g., with the following slow way to compute Euler's phi
   f <- function(n) sum(sapply(seq_len(n), function(i)1/i)) - log(n)
I get
  > timeOut(f(1e5), elapsed=1)
  [1] 0.5772207
  > timeOut(f(1e6), elapsed=1)
  Error in FUN(1:1000000[[711624L]], ...) : reached elapsed time limit
Use try() or tryCatch() to check for the error.  E.g.,
  > sapply(1:7, function(n)tryCatch(timeOut(f(10^n), elapsed=1), error=function(e)-1))
  [1]  0.6263832  0.5822073  0.5777156  0.5772657  0.5772207 -1.0000000 -1.0000000
You could look at 'e' in the error handler to see if it is a time out problem.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Bert Gunter
> Sent: Thursday, March 15, 2012 3:05 PM
> To: Ramiro Barrantes
> Cc: r-help at r-project.org
> Subject: Re: [R] Timer on a function
> 
> On Thu, Mar 15, 2012 at 2:24 PM, Ramiro Barrantes
> <ramiro at precisionbioassay.com> wrote:
> >    Hello,
> >
> > I have a program that consists of a loop fitting a function over many
> models.  Sometimes the fitting on a particular model takes minutes to converge.  Is there
> a way that I can limit the amount of time that R spends on a given model:
> 
> AFAIK, no -- this is an OS level issue.
> 
> Of course, most iterative fitting procedures have controls for the
> number of iterations, convergence criteria, etc. , but there is no
> awareness of timing except when the OS is interrogated, e.g. by
> ?proc.time or ?system.time . Such calls would have to be built into
> the fitting function or OS level services would have to be invoked to
> run the R process with timing limitations built in. See e.g. ?Rscript
> for one possible approach.
> 
> Corrections or clever tricks to get around these perceived limitations
> welcomed, of course.
> 
> -- Bert
> 
> Cheers,
> Bert
> >
> > say if my line is:
> >
> > fittingFunction( func, model.1)
> >
> > can I have some function:
> >
> > stopIfUnderTime(  fittingFunction( func, model.1) , 5 )
> >
> > where stopIfUnderTime will return the result if it finishes under 5 seconds, or NA
> otherwise.
> >
> > Is there anything like this?  (just looked through the web but did not find anything)
> >
> > Thanks,
> > Ramiro
> >
> >
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > 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.
> 
> 
> 
> --
> 
> Bert Gunter
> Genentech Nonclinical Biostatistics
> 
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-
> biostatistics/pdb-ncb-home.htm
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list