Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Speeding up the use of system.out

 
Tina Viessmann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use System.out.println(), use a proper logging framework.
 
Tina Viessmann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...which of course a logging framework will also do (and in a much more configurable way).
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18218
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Geert Pante
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1540
18
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out is a PrintStream.
 
Geert Pante
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1540
18
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another suggestion is to use StringBuilder to create larger chunks of character data to output all at once.
 
Paul Clapham
Sheriff
Posts: 21140
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic