Wednesday, July 20, 2011

How to disable System.out?

I have a legacy code base with tons of System.out.println(). Actually, I don't need this in production, but during development it's cool enough to have. So, I've decided to disable System.out:
package org.halyph;


public class DisableMain {
	public static void main(String[] args) {
		PrintStream printStreamOriginal = System.out;

		boolean DEBUG = true;
		if (!DEBUG) {
			System.setOut(new PrintStream(new OutputStream() {
				public void close() {}
				public void flush() {}
				public void write(byte[] b) {}
				public void write(byte[] b, int off, int len) {}
				public void write(int b) {

		long a = System.currentTimeMillis();
		for (int i = 0; i < 1000000; i++) {

		long b = System.currentTimeMillis() - a;


So, plain looped println required ~10 seconds on my laptop, and only ~1 second with null output stream.

1 comment:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.