System.out calls

H

hiwa

Bruce Lee ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
Do frequent System.out calls hamper performance?
Yes, even a single one does. You should avoid the use
of System.out at all cost.

Jokes aside, what kind of application are you talking
about?
 
R

Ross Bamford

Do frequent System.out calls hamper performance?

Well, System.out is synchronized, so yes, frequent calls will cause some
bottlenecking. If you really overuse it, it's easy to destroy any benefits
you gain from multithreading. And output in general is _slow_ compared to
memory operations.

As Roedy (rightly) advises, it's best to store up output for a single
System.out call. One common way is to have a buffered writer with manual
flush, backed by System.out and only flushed every so often.
 
C

Chris Uppal

Ross said:
[...] And output in general is _slow_ compared to
memory operations.

And output to a TTY (in Unix) or to a Windows "console" can be /very/ slow even
compared to I/O in general.

-- chris
 
T

Thomas G. Marshall

Ross Bamford coughed up:
Well, System.out is synchronized, so yes, frequent calls will cause
some bottlenecking.

*ONLY* if there are different threads attempting the system.out.{whatever}at
the same time! It is a slow beast anyway, simply because it is i/o, but you
may be misleading the newbie a little. A synchronized method doesn't block
anything other than what it is synchronizing on, in this case all the
java.io.PrintStream methods synchronize on their own instances.
 
R

Ross Bamford

Ross Bamford coughed up:

*ONLY* if there are different threads attempting the
system.out.{whatever}at
the same time! It is a slow beast anyway, simply because it is i/o, but
you
may be misleading the newbie a little. A synchronized method doesn't
block
anything other than what it is synchronizing on, in this case all the
java.io.PrintStream methods synchronize on their own instances.

True, I should have been clearer that this applies specifically to
multithreaded programs, where System.out is called from different threads.
The time taken to actually obtain a (previously open, obviously)
synchronization lock is vanishingly small, so it's not an _automatic_
performance hit (that comes from the IO ;))

Apologies for the confusion.
 
T

Thomas G. Marshall

Chris Uppal coughed up:
Ross said:
[...] And output in general is _slow_ compared to
memory operations.

And output to a TTY (in Unix) or to a Windows "console" can be /very/ slow
even compared to I/O in general.

-- chris

Coding from your 110 baud teletype again? ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top