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?
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.
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.
An IDE is no substitute for an Intelligent Developer.
Understanding the scope of the problem is the first step on the path to true panic
Joined: Feb 03, 2011
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.
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?