3

Does Java run any code for System.out.println(...) when I run the program in a console-less GUI? (where I don't redirect the program output to anywhere)

In fact I'd like to know if System.out.println(...) can affect the program performance when it is run by clicking the jar file.

Consider that I print thousands of lines per minute.

Johnny
  • 1,509
  • 5
  • 25
  • 38
  • 1
    The best way to answer questions about performance is to do the metric yourself. What happened when you created a small program with a bunch of print statements and the same program without them? Did you notice any performance change between those example programs? – Kevin Workman Jan 02 '14 at 20:01
  • From my experience, yes too both. We found if we redirected the stdout and stderr through a Null stream (which simply consumed the streams), we could gain a not insignificant improvement ... But the content been sent to stdout was not small either... – MadProgrammer Jan 02 '14 at 20:01
  • Of course code is being run. Use a logging library instead - at least then it's a no-op if you want it to be. – Brian Roach Jan 02 '14 at 20:03

6 Answers6

2

The way Java deals with calls, there probably will be significant impact if you println something that's not a simple string.

For example,

System.out.println("Iteration " + i);

creates a StringBuilder, converts an int and copies a bunch of characters before it can even decide that there is nothing behind System.out.

1

Let me put it this way.

If you were to execute you program from another program, then you would be able to read the stdout of your program, so yes, there is always something sent to stdout even if nothing is "listening"

From experience, we found there was an improvement in performance (under windows), if redirected the stdout and stderr thur a null stream (which consumed the streams), but we were dealing with a lot of output not just from our program but also the RMI server we were communicating with

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

you are writing to standard out, if there is no listener for that stream then no worries.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • I think the question is asking more so if, putting stuff on the stream with no listeners has any downside – Cruncher Jan 02 '14 at 20:01
  • This answer is incorrect. Just having an expression to evaluate as the argument of `println` is overhead, plus the fact that `PrintStream` is a *synchronized* class, with far-reaching consequences, such as the precluding of some important JIT-compiler optimizations. – Marko Topolnik Jan 02 '14 at 20:18
0

By looking here it looks like that multiple method calls are done inside println before any check on whether or not someone is listening the streaming.

THEN:

For this purpose I'd prefer to create a Debug class with a print method as I'm totally sure that it doesn't affect performance:

public class Debug {
    public static boolean debug = true;
    public static int debLev = 5;

    public static void print(String stringa){
        if(debug)
            System.out.println(stringa);
    }

    public static void print(String stringa, int level){
        if(debug && level >= debLev){
            System.out.println(stringa);
        }
    }
}

In this way you can disable printing and also you can filter debug printing by severity.

HAL9000
  • 3,562
  • 3
  • 25
  • 47
0

theoretically yes, It can affect performance since it is in fact a method call and method calls use minute amounts of processing power. However on a modern computer It will have no affect to general performance, In fact most modern programs/games use println's to debug their program even in real releases. I would leave them there if they are a necessity.

snocavotia
  • 435
  • 1
  • 3
  • 13
  • Are you sure that most modern programs release with debug output? – bblincoe Jan 02 '14 at 20:04
  • 1
    I am. Have you taken a look at the debug code for the Forge project that works on minecraft? they use System.out.println and System.err.println alot to debug. As well as many games that I have came in contact with in Java. – snocavotia Jan 02 '14 at 20:05
0

See the docs on Out here:

http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#out

Even if there isn't an listener on the out stream you're still going to have overhead from the calls. Your performance effects will depend largely on the amount of this your doing. If there is a listener, then you're going to see much higher performance impact.

TLDR: The compiler isn't going to remove the system.out.println call, so you'll see some marginal performance hit.

Nathaniel D. Waggoner
  • 2,856
  • 2
  • 19
  • 41