Last active
October 31, 2016 17:00
-
-
Save MLLeKander/b0016e7ff670cb54c21361534639e29c to your computer and use it in GitHub Desktop.
IO Output Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ javac TestIO.java && java TestIO > out.txt | |
1783293664 | |
raw System.out.println: 26941490983 | |
raw System.out.print: 12888388868 | |
PrintWriter(System.out): 867392610 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class TestIO { | |
public static void testSystemOutPrintln() { | |
for (int i = 0; i < 10_000_000; i++) { | |
System.out.println(i); | |
} | |
} | |
public static void testSystemOutPrint() { | |
for (int i = 0; i < 10_000_000; i++) { | |
System.out.print(i+"\n"); | |
} | |
} | |
public static void testPrintWriter() { | |
PrintWriter out = new PrintWriter(System.out); | |
for (int i = 0; i < 10_000_000; i++) { | |
out.println(i); | |
} | |
out.flush(); | |
} | |
public static void main(String[] args) { | |
int out = 0; | |
// Warm-up | |
for (int i = 0; i < 1_000_000; i++) { | |
out += i; | |
} | |
System.err.println(out); | |
long t1, t2; | |
System.gc(); | |
t1 = System.nanoTime(); | |
testSystemOutPrintln(); | |
t2 = System.nanoTime(); | |
System.err.printf("raw System.out.println: %d\n",t2-t1); | |
System.gc(); | |
t1 = System.nanoTime(); | |
testSystemOutPrint(); | |
t2 = System.nanoTime(); | |
System.err.printf("raw System.out.print: %d\n",t2-t1); | |
System.gc(); | |
t1 = System.nanoTime(); | |
testPrintWriter(); | |
t2 = System.nanoTime(); | |
System.err.printf("PrintWriter(System.out): %d\n",t2-t1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment