-3

Why system.out.println() Method output the value first then user Define output class..?

public class FastIO {
        static class FastReader {...}

        static class FastWriter {
            private final BufferedWriter bw;

            public FastWriter() {
                this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }

            public void print(Object object) throws IOException {
                bw.append("").append(String.valueOf(object));
            }

            public void println(Object object) throws IOException {
                print(object);
                bw.append("\n");
            }

            public void close() throws IOException {
                bw.close();
            }
        }

        public static void main(String[] args) {
            try {
                //FastReader reader=new FastReader();
                FastWriter writer = new FastWriter();
                    long startTime;
                    long endTime;

                    startTime=System.currentTimeMillis();
                    writer.println("Print String Using FastIO");
                    endTime=System.currentTimeMillis();

                    writer.println("Time taken by FastIO: "+(endTime-startTime)+"ms");

                    startTime=System.currentTimeMillis();
                    System.out.println("Print String Using System.out");
                    endTime=System.currentTimeMillis();
                    writer.println("Time taken by System.out: "+(endTime-startTime)+"ms");
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

}

Output :
Print String Using System.out
Print String Using FastIO
Time taken by FastIO: 0ms
Time taken by System.out: 0ms

Progman
  • 16,827
  • 6
  • 33
  • 48
  • 3
    That's not how you write benchmarking code in Java. Relevant: [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Hovercraft Full Of Eels Apr 14 '22 at 17:44
  • 3
    Your `FastWriter` is using a buffered writer on top of the already-buffered `System.out` (which would be slower). I'm not sure of the specific semantics at play here, but I'd assume that's the primary factor. – Rogue Apr 14 '22 at 17:46
  • It has nothing to do with benchmarking. I'm just trying something else with that. Okay, I understand that. here I'm try to write a FastIO class for a coding competition and when I try to output some statement to the console. The output is not in particular order(sequence). How can I resolve this issue.? – Gaurav Kumar Apr 15 '22 at 07:26

1 Answers1

0

Everything that you write into the FastWriter is buffered in its BufferedWriter bw until either the buffer in bw overflows or the FastWriter is closed. Only then is it written out to System.out.

This is the sequence:

writer.println("Print String Using FastIO");  // this is buffered in writer.bw
// ...
writer.println("Time taken by FastIO: "+(endTime-startTime)+"ms");  // this is buffered in writer.bw
// ...
System.out.println("Print String Using System.out");  // this is directly written to System.out
// ...
writer.println("Time taken by System.out: "+(endTime-startTime)+"ms");  // this is buffered in writer.bw
writer.close();  // now the 3 lines buffered in writer.bw are written to System.out
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34