From luke-tierney at uiowa.edu  Sun Jul 17 18:13:10 2016
From: luke-tierney at uiowa.edu (luke-tierney at uiowa.edu)
Date: Sun, 17 Jul 2016 11:13:10 -0500
Subject: [Rd] cat() in system.time() ?
In-Reply-To: <22408.58228.665116.384597@stat.math.ethz.ch>
References: <577A8594.9070408@gmail.com>
	<22408.58228.665116.384597@stat.math.ethz.ch>
Message-ID: <alpine.DEB.2.10.1607171110570.2693@luke-Latitude>

On Fri, 15 Jul 2016, Martin Maechler wrote:

> Hi Ben (and everyone else),
>
> as this did not attract attention yet, let me start
>
>>>>>> Ben Bolker <bbolker at gmail.com>
>>>>>>     on Mon, 4 Jul 2016 11:49:40 -0400 writes:
>
>    > Does anyone know if there's a reason that proc.time() uses cat()
>    > rather than message() to print the output when there has been an error
>    > in the process of timing?
>
> This is really not about proc.time(), but  about  system.time()
> [ and I have corrected the  'Subject' accordingly ] ..
>
>    > line 31 of time.R,
>
>    > https://github.com/wch/r-source/blob/e5b21d0397c607883ff25cca379687b86933d730/src/library/base/R/time.R#L31
>
>    > on.exit(cat("Timing stopped at:", ppt(proc.time() - time), "\n"))
>
>    > This means that as far as I can tell the general way to make sure
>    > there is no output from a timed statement is ...
>
>    > tt1 <- capture.output(tt0 <- suppressMessages(suppressWarnings(
>    >					try(<stuff to try>, silent=TRUE))))
>
>    > (I know I could/should be using tryCatch() instead of try(), but I don't
>    > think it really matters here ... ?)
>
>    > What would people think of a request to change this to message()
>    > rather than cat() in the future ... ?  (This would mess up code that is
>    > already using capture.output() to store this information ...)
>
> [I think that (last issue) would be acceptable.]
>
> One reason of the current cat() may just be historical:
> message() did not exist yet when  system.time()  was created.
> However, I agree that that is not good enough a reason to keep
> it. Much more important is the fact that it is *nice* that the
> message
>
>   Timing stopped at: ...
>
> is printed in many cases when a system.time()d call is stopped
> early.  Quite often for me this is *not* when an error happens
> as your suppress*() contortions (;-) suggest, but rather when I
> interrupt the long lasting call.
> And the current setup nicely gives
>
>   > i <- 0; system.time( while(TRUE) i <- i+1 )
>     C-c C-c
>   Timing stopped at: 1.001 0.084 1.086
>   >
>
>
> However, at least this simple case, also works fine with
> message() instead of cat() ... as I just tried now.
>
> A harder case are the "bad errors", e.g.,  memory overflow and
> similar bad things...
> cat() seems to be pretty robust, where as message() does invoke
> a handler (exactly *why* you want it, right?) and that may be
> harder to keep working correctly after certain errors than a
> simple cat().
>
> I hope that some real experts (on "context switching", "long
> jumps", etc) would chime in now.

I don't have a strong opinion on whether message would be better or
not.  But now that the on.exit code is executed after unwinding the C
call back to the point of the system.time call there is no longer such
a robustness issue.

Best,

luke


>
> Martin
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney at uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



[Rd] cat() in system.time() ?
luke-tierney at uiowa.edu