From hb at biostat.ucsf.edu  Fri Jan  2 21:00:08 2015
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Fri, 2 Jan 2015 12:00:08 -0800
Subject: [Rd] Benchmark code, but avoid printing
In-Reply-To: <CABtg=KmgV-6QEffrGc4HzOSUM-jvE3ExHFB1KefAgv16S=+oGA@mail.gmail.com>
References: <CABtg=KmgV-6QEffrGc4HzOSUM-jvE3ExHFB1KefAgv16S=+oGA@mail.gmail.com>
Message-ID: <CAFDcVCSe+tp=aW=xoVXW2p3eUuj6HKer6jd-e+86FHEG0LBdRw@mail.gmail.com>

On Fri, Jan 2, 2015 at 9:02 AM, G?bor Cs?rdi <csardi.gabor at gmail.com> wrote:
> Dear all,
>
> I am trying to benchmark code that occasionally prints on the screen
> and I want to
> suppress the printing. Is there an idiom for this?
>
> If I do
>
> sink(tempfile)
> microbenchmark(...)
> sink()
>
> then I'll be also measuring the costs of writing to tempfile. I could
> also sink to /dev/null, which is probably fast, but that is not
> portable.

Interesting problem.  On Windows NUL corresponds to /dev/NULL, e.g.
con <- file("NUL", open="wb").  Not that it's cross platform, but it
at least allows you to cover on more OS.  Maybe R should have a
built-in "null" device.  An easier solution is probably to go back to
the maintainers of the functions outputting text and ask them for an
option to disable that.

>
> Is there a better solution? Is writing to a textConnection() better?

For large number of output *lines* (not characters), textConnection()
is exponentially slow (at least in R 3.1.0).  Use rawConnection()
instead, cf. http://www.jottr.org/2014/05/captureOutput.html

/Henrik

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



[Rd] Benchmark code, but avoid printing
Henrik Bengtsson