Disable System.out for speed in Java

Again an output stream gobbler could work, something perhaps like...

System.setOut(new PrintStream(new OutputStream() {
    @Override
    public void write(int arg0) throws IOException {

    }
}));

But a better solution is to use a formal logging utility as has already been mentioned.


you should use a real logging framework like log4j, or slf4j. in either case, you can set the logging level, to restrict the level of information being dumped into the logs.

if you insist in using System.out, you can wrap your logging statements in a condition, like,

if (DEBUG) {
  System.out.println(...);
}

and yes, heavy logging or printing to System.out will affect performance.


I agree with others that a proper logger should be used. However that is not possible in every case. The following code disables out and is fast as OP asked:

System.setOut(new java.io.PrintStream(new java.io.OutputStream() {
    @Override public void write(int b) {}
}) {
    @Override public void flush() {}
    @Override public void close() {}
    @Override public void write(int b) {}
    @Override public void write(byte[] b) {}
    @Override public void write(byte[] buf, int off, int len) {}
    @Override public void print(boolean b) {}
    @Override public void print(char c) {}
    @Override public void print(int i) {}
    @Override public void print(long l) {}
    @Override public void print(float f) {}
    @Override public void print(double d) {}
    @Override public void print(char[] s) {}
    @Override public void print(String s) {}
    @Override public void print(Object obj) {}
    @Override public void println() {}
    @Override public void println(boolean x) {}
    @Override public void println(char x) {}
    @Override public void println(int x) {}
    @Override public void println(long x) {}
    @Override public void println(float x) {}
    @Override public void println(double x) {}
    @Override public void println(char[] x) {}
    @Override public void println(String x) {}
    @Override public void println(Object x) {}
    @Override public java.io.PrintStream printf(String format, Object... args) { return this; }
    @Override public java.io.PrintStream printf(java.util.Locale l, String format, Object... args) { return this; }
    @Override public java.io.PrintStream format(String format, Object... args) { return this; }
    @Override public java.io.PrintStream format(java.util.Locale l, String format, Object... args) { return this; }
    @Override public java.io.PrintStream append(CharSequence csq) { return this; }
    @Override public java.io.PrintStream append(CharSequence csq, int start, int end) { return this; }
    @Override public java.io.PrintStream append(char c) { return this; }
});

My sample measurements were:

  • 82ms full blown output
  • 57ms when only overriding write(int) in OutputStream
  • 31ms when overriding PrintStream methods as well
  • 5ms when commented out all println and printf usages

So it is faster than other answers here, but can't beat non-existent code (commenting out System.outs or wrapping in if (DEBUG)) as varargs allocates arrays and boxes primitives or StringBuilder (+ too) still executes.