package ch.claude_martin.playground; import java.io.FilterOutputStream; import java.io.OutputStream; import java.io.PrintStream; import java.lang.reflect.Field; public final class SomeClass { final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE }; static int stringSize(int x) { for (int i = 0;; i++) if (x <= sizeTable[i]) return i + 1; } static boolean increment(byte[] buf, int off) { if (buf[off] == 0) { buf[off] = '1'; return true; } if (buf[off] == '9') { buf[off] = '0'; return increment(buf, off - 1); } buf[off]++; return false; } public static void main(String... args) throws Throwable { final long start = System.nanoTime(); final int n = 10000000; // IntStream.rangeClosed(0, lastNumber).forEach(System.out::println); final int size = stringSize(n); byte[] buf = new byte[size + 1]; buf[size-1] = '0'; buf[size] = '\n'; final Field field = FilterOutputStream.class.getDeclaredField("out"); field.setAccessible(true); OutputStream out = (OutputStream) field.get(System.out); int off = size - 1; final int last = size - 1; // least significant byte for (int i = 0; i < n; i++) { if (increment(buf, last)) off--; out.write(buf, off, size - off+1 ); } final long end = System.nanoTime(); System.out.println("===="); System.out.println(end - start); } }