• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Speeding up the use of system.out

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...which of course a logging framework will also do (and in a much more configurable way).
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Geert Pante
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another suggestion is to use StringBuilder to create larger chunks of character data to output all at once.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic