import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class ConsolePrint { private static String line1 = "[stdout] very very long line very very long line very very long line very very long line very very long line very very long line very very long line very very long line"; private static String line2 = "[file] very very long line very very long line very very long line very very long line very very long line very very long line very very long line very very long line\n"; private static String line3 = "[/dev/stdout] very very long line very very long line very very long line very very long line very very long line very very long line very very long line very very long line\n"; public static void main(String[] args) throws IOException { int count = Integer.valueOf(args[0]); long t1 = stdout(count); File f1 = File.createTempFile("test", "log"); f1.deleteOnExit(); long t2 = file(f1, line2, count); File f2 = new File("/dev/stdout"); long t3 = file(f2, line3, count); System.out.println("lines: " + String.format("%,d", count)); System.out.println("System.out.println: " + String.format("%,d", t1) + " ms"); System.out.println("file: " + String.format("%,d", t2) + " ms"); System.out.println("/dev/stdout: " + String.format("%,d", t3) + " ms"); } private static long stdout(int count) { long start = System.currentTimeMillis(); for (int i = 0; i < count; i++) { System.out.println(line1); } long end = System.currentTimeMillis(); return end - start; } private static long file(File file, String line, int count) throws IOException { byte[] bytes = line.getBytes(); try ( FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024 * 4); ) { long start = System.currentTimeMillis(); for (int i = 0; i < count; i++) { bos.write(bytes); } bos.flush(); long end = System.currentTimeMillis(); return end - start; } } }