From: Tom Anderson on
Greets yalls,

Given:

OutputStream out; // this is not buffered

Which is better:

Writer w = new BufferedWriter(new OutputStreamWriter(out));
Writer w = new OutputStreamWriter(new BufferedOutputStream(out));

?

I suppose i'm wondering about (a) whether encoding characters to bytes is
more efficient in bulk than in drips and drabs (and i suppose it must be,
from cache effects alone, but how much so?) and (b) what happens when a
lot of translated characters get written to an unbuffered stream - do the
bytes go in character-by-character (IYSWIM), or in bulk?

The thing that niggles me is that BufferedWriter has a newLine() method,
so i tend to use that for its convenience. But if it's sitting on top of
an OutputStream, then i might need buffering there as well.

I know i could go and read the source code, or do some measurements, and
give me a day or so and i probably will, but this is something i've been
wondering about, so i thought i'd share!

tom

--
Due to my inability to deploy my Taser, I had to resort to punching him
as hard as I could in the face. It is a taught technique. -- PC 5566,
on the arrest of Yassin Omar
From: Roedy Green on
On Sat, 20 Feb 2010 11:48:43 +0000, Tom Anderson
<twic(a)urchin.earth.li> wrote, quoted or indirectly quoted someone who
said :

>Writer w = new BufferedWriter(new OutputStreamWriter(out));
>Writer w = new OutputStreamWriter(new BufferedOutputStream(out));

This can be most easily answered by experiment. While you are at it
figure it out for InputStreamReader too. You must run them many times
to make sure you are not getting fooled by JVM startup time/caching
artifacts.

Why would Sun invent BufferedWriters if it is more efficient not to
use them?

When you find out, let us know. I will teach the FileIO Amanuensis to
to it the preferred way.

see http://mindprod.com/applet/fileio.html

If you don't want to do the work, I will. You have revived an ancient
curiosity itch even if the answer may make no practical difference.

The benchmark could create 500 MB file encoded in UTF-8 with cyclic
16-bit chars (to give the encoder something to do), then read it and
average the read/write time for the alternative ways of coding it over
5 trials.

It is a gene programmers have. Whenever there are two or more ways of
doing something, they HAVE to know which is the best way.
--
Roedy Green Canadian Mind Products
http://mindprod.com

When a newbie asks for help tracking a bug in a code snippet, the problem is usually is the code he did not post, hence the value of an SSCCE.
see http://mindprod.com/jgloss/sscce.html
From: Roedy Green on
On Sat, 20 Feb 2010 11:48:43 +0000, Tom Anderson
<twic(a)urchin.earth.li> wrote, quoted or indirectly quoted someone who
said :

>Writer w = new BufferedWriter(new OutputStreamWriter(out));
>Writer w = new OutputStreamWriter(new BufferedOutputStream(out));

It turns out it makes a huge difference. I will post code shortly.
--
Roedy Green Canadian Mind Products
http://mindprod.com

When a newbie asks for help tracking a bug in a code snippet, the problem is usually is the code he did not post, hence the value of an SSCCE.
see http://mindprod.com/jgloss/sscce.html
From: Lew on
Tom Anderson wrote, quoted or indirectly quoted someone who said :
>> Writer w = new BufferedWriter(new OutputStreamWriter(out));
>> Writer w = new OutputStreamWriter(new BufferedOutputStream(out));

Roedy Green wrote:
> It turns out it makes a huge difference. I will post code shortly.

Please also show the environment and which one won.

I wonder if things like RAM, CPU, disk subsystem performance and such
influence the outcome.

--
Lew
From: Roedy Green on
On Sat, 20 Feb 2010 11:48:43 +0000, Tom Anderson
<twic(a)urchin.earth.li> wrote, quoted or indirectly quoted someone who
said :

>Writer w = new BufferedWriter(new OutputStreamWriter(out));
>Writer w = new OutputStreamWriter(new BufferedOutputStream(out));

one thing to be aware of is BufferedWriters measure their buffers in
chars and BufferedOutputStreams measure them in bytes.
--
Roedy Green Canadian Mind Products
http://mindprod.com

When a newbie asks for help tracking a bug in a code snippet, the problem is usually is the code he did not post, hence the value of an SSCCE.
see http://mindprod.com/jgloss/sscce.html