Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Last active October 31, 2016 17:00
Show Gist options
  • Save MLLeKander/b0016e7ff670cb54c21361534639e29c to your computer and use it in GitHub Desktop.
Save MLLeKander/b0016e7ff670cb54c21361534639e29c to your computer and use it in GitHub Desktop.
IO Output Test
$ javac TestIO.java && java TestIO > out.txt
1783293664
raw System.out.println: 26941490983
raw System.out.print: 12888388868
PrintWriter(System.out): 867392610
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