[R] How to defeat buffering in Rgui?

Peter Langfelder peter.langfelder at gmail.com
Fri Jun 29 19:25:13 CEST 2012


On Fri, Jun 29, 2012 at 10:15 AM, Spencer Graves
<spencer.graves at structuremonitoring.com> wrote:
> Dear Prof. Ripley:
>
>
>      Thanks for the reply.  Unfortunately, flush.console() seems to lock up
> my system.  I tried the following:
>
>
> for(i in 1:1e7){
>    tst <- sin(i)
>    if((i%%1e5)==0)cat(i, "")
>    if((i%%1e6)==0)cat('\n')
>    flush.console()
> }
>
>
>      This slows down Rgui 2.15.1 (32-bit) by a factor of roughly 350:  In
> Rterm (64-bit), on my dual core 64-bit Windows 7 system, this ran to
> completion in 1 minute;  this same code without flush.console() ran to
> completion in 35 seconds in Rgui 2.15.1 (64-bit), then presented the
> display.  Watching it run in Rgui, it seemed to consume roughly half of one
> CPU while making very slow progress.  I started timing it when it displayed
> 200000;  24 minutes later, I noticed it was displaying 900000.  That
> produces an estimate of 340 minutes to complete.
>
>
>      I'd like to use this to give users (e.g., of a CRAN package) a progress
> report on long computations:  Otherwise, a user doesn't know if the
> computation will every complete.
>
>
>      Suggestions?

Only do flush.console() when you actually print something:

if((i%%1e5)==0) flush.console()

As a system call, flush.console() takes some time, and you do it
unnecessarily in each pass through your loop even though you only
print something out once every 10^5 passes.

Peter



More information about the R-help mailing list