2

Which is the fastest way to write to System.out?
I know 3 ways so far:
1. the classic one:

for (int i = 0; i < 100000; i++) {
    System.out.println( i );
}

2. Using StringBuilder:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++) {
    sb.append(i).append("\n");
}
System.out.print(sb.toString());

3. Wrapping System.out in a Buffer like:

OutputStream out = new BufferedOutputStream(System.out);
for (int i = 0; i < 100000; i++) {
    out.write((i + "\n").getBytes());
}
out.flush();  

Is there any method that is more efficient than all of these?

Csa77
  • 649
  • 13
  • 19
Jack91255
  • 59
  • 4
  • 5
    Competitive java programming is rarely about who can print hundreds of thousands of lines to the console the fastest. – Michael Aug 24 '18 at 13:01

0 Answers0