aspose file tools*
The moose likes Performance and the fly likes Speeding up the use of system.out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Speeding up the use of system.out" Watch "Speeding up the use of system.out" New topic
Author

Speeding up the use of system.out

Tina Viessmann
Greenhorn

Joined: Jun 01, 2011
Posts: 8
Hi,

I need to write a very large block of text on system.out. I would prefere to do that in the most performant way. How is that done?

The text is generated step by step while processing different information. Therefore I can think of three ways to handle the output:
- Write out every part of the when immediately when it's created.
- Buffer the whole text (using a StringBuffer) and write it out as whole when the processing has finished.
- Use a mix of both approaches: Buffer a larger amount (the result of several processing steps) of text, write it out, go on buffering, write again and so on.

What's the best approach? How else can it be done?

Thanks in advance!
Tina
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Don't use System.out.println(), use a proper logging framework.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Tina Viessmann
Greenhorn

Joined: Jun 01, 2011
Posts: 8
My fault, I should have said that right from the start: I'm not logging anything.
I'm producing kind of a html page. I need to write my output on the standard output stream, because it's a requirement of the system my program runs on.
Christopher Nortje
Greenhorn

Joined: Jul 09, 2010
Posts: 16
Remember that System.out is an variable the refers to an OutputStream, so just write to the stream as soon as the "text" become available.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

...which of course a logging framework will also do (and in a much more configurable way).
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Not to pop your bubble, Paul, but even die-hard logging fanatics like me don't usually use the log facility to write text data. For one thing, if the point is to create HTML text data, the prologue info (timestamp, etc.) is going to be objectionable to the consumer (HTML output stream client).

To get the ultimate in outputstream performance, it's a good idea to examine the entire pipeline, start to finish.

Since System.out is a predefined channel, the first thing I'd do is introspect it for its characteristics (buffering strategies, buffer sizes, and so forth). This will probably not be as optimal as if you were using a general-purpose channel, but stdout does have its benefits. You may be able to override some of the performance-related parameters of the System.out channel, but I've not had occasion to do that myself.

Once you know how the buffering scheme works, you can work backwards from there. You may or may not see benefits from pre-building the output in a StringBuilder, since the output stream processor may simply duplicate that work.

Of course the real test is the real-world performance, since the makeup of the data and the overall system environment can both have an impact. So measure and tune accordingly.


Customer surveys are for companies who didn't pay proper attention to begin with.
Geert Pante
Greenhorn

Joined: Feb 03, 2011
Posts: 8
Christopher Nortje wrote:Remember that System.out is an variable the refers to an OutputStream, so just write to the stream as soon as the "text" become available.


If you want buffering, just write to .
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 189

System.out is a PrintStream.
Geert Pante
Greenhorn

Joined: Feb 03, 2011
Posts: 8
Carey Brown wrote:System.out is a PrintStream.


OK, I see your point. If you wrote your code to use println functions, it's easier to replace with


Mark that I can't guarantee this will be faster.
This only makes sure that the output is not flushed automatically, but only after a certain amount of bytes is written. The default size of a BufferedOutputStream is 4096, IIRC.

Greets, Geert.
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 189

Another suggestion is to use StringBuilder to create larger chunks of character data to output all at once.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

It's not clear to me why you want this to be faster. Isn't there going to be somebody reading that output, since you are sending it to the console? In which case shouldn't you send it slower, so they can actually read it before it scrolls off the top of the screen?
 
jQuery in Action, 2nd edition
 
subject: Speeding up the use of system.out