File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Application performance issue with System.out.println(); statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Application performance issue with System.out.println(); statement" Watch "Application performance issue with System.out.println(); statement" New topic
Author

Application performance issue with System.out.println(); statement

jawaharlal vislawath
Greenhorn

Joined: May 09, 2012
Posts: 3


Hi Friends,

Is System.out.println(); statement indeed hampers the performance of the application? Please give the valuable suggestions..

Thanks,
Jawahar
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

jawaharlal vislawath wrote:Is System.out.println(); statement indeed hampers the performance of the application?

Not so much as you'd notice. And if you need it, what else would you do?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
It really depends - compared to what? What else does the application do? There are some applications where yes, System.out.println() will slow the application down considerably. There are many, many other applications where the effect is insignificant. The best way to find out for your application would be to simply measure the performance with the println() statements, then comment them out and measure again, without those same statements. But as Winston alludes - do you need them, or not?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

I would think that EVERY line of code that gets executed impacts performance to some degree.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2448
    
  28

The System.out.println eventually gets translated to a write operation on some sort of buffer that drives the monitor. The write to the buffer should be very quick, but of course as others have said "COmpared to what". If you log a message every time you add 2 numbers, then you are going to spend more time in System.out . However, if you are logging a message everytime you run a complicated database query on 1 million rows, then the database query is going to take more time.

However, one thing you can be sure of is that system.out will cause contention. The buffer will be protected somehow from being accesses by multiple threads at the same time by a hardware/OS level lock (and might even be protected by multiple locks). So, if you have a large number of threads executing in parallel, they will lock each other up on the System.outs. Adding a lot of System.out is like adding a lot of synchronized blocks. Your multithreaded app is going to slow down because threads will be waiting on each other.
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 163

Jayesh explained very nice. I would just recommend for general propose to use some Log framework which they are made to solve these problems (by the way, I think i saw a benchmark which shows that logback have the highest throughput ). If you are building/working on a system with tons of users, you probably would need some more elaborated solution.


Please, visit me for some cool tech post at www.ourdailycodes.com
jawaharlal vislawath
Greenhorn

Joined: May 09, 2012
Posts: 3
Thank you all for commenting on this post, Jayesh/Cestari indeed good view on alternate for this, as we need to use logger.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Application performance issue with System.out.println(); statement