Re: Why is Java so slow????
Bent C Dalager wrote:
In article <tPo0j.60281$RX.60008@newssvr11.news.prodigy.net>,
Mark Space <markspace@sbc.global.net> wrote:
Mark Space wrote:
String sed = "0000000\n";
This *still* causes the output to be flushed. Remove the \n and I get a
50% speed increase (halves runtime).
This could be because your console window needs to scroll less?
Is there no way to set the
autoflush for System.out to false?
System.setOut(new PrintStream(System.out));
might work, after a fashion :-)
I don't think this works the way you think it will.
First, System.out already is a PrintStream. When a PrintStream is
created, the constructor just takes the existing object, and stores it
in its "out" instance variable. So you end up with a new PrintStream,
that contains another PrintStream (the original System.out object) which
already has the autoflush variable set to true.
I haven't tried it, but I bet that is what the debugger would say.
Note that this is weird too:
OutputStream os = new BufferedOutputStream( System.out );
Guess what this makes?
Same as your idea, it makes a BufferedOutputStream that simply wraps the
existing PrintStream which is System.out (autoflush still set to true!).
System.out already wraps a BufferedOutputStream, however. And that
BufferedOutputStream wraps a FileOutputStream. Finally, something that
doesn't wrap something else! I think FileOutputStream handles all the
real work of writing characters to the OS.
Why is wrapping with a second BufferedOutputStream so much faster? I
dunno, but I'm guessing it does buffer, and it does cut down the calls
to PrintStream. PrintStream will still autoflush though, just perhaps
less often.