[Rd] Benchmark code, but avoid printing

Henrik Bengtsson hb at biostat.ucsf.edu
Fri Jan 2 21:00:08 CET 2015


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



More information about the R-devel mailing list