0

When I mention "better", it means the program will reduce its use of memory, or the program will run faster.

In competetive coding, we seek better IO and reduce time and space complexity to access sample cases. I use Java specifically in competetive coding, and I wonder is there really a difference between using a input class or using java.io.BufferReader.

When I reviewed past USACO platinum problem solution written in Java, I saw uses of BufferedReader.

If I say I use BufferReader, the code will be:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class template {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    }
}

If I write a input class, the code will be (credit to: Department of Computer Engineering in Kasetsart University)

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}

I expected directly using BufferedReader could be better.

LLLgoyour
  • 11
  • 3
  • 1. If you want **specific** answers about performance, you have to benchmark (in this case [micro-benchmark](https://stackoverflow.com/questions/504103)) a *specific* use case yourself to get useful answers. General, vague answers are of limited use. 2. despite #1 I will tell you that the majority of the performance in any I/O will be lost in the actual I/O and not in any utility code surrounding it (unless that utility code does something *very* silly, like re-reading the same file over and over again). 3. being able to write code faster is also valuable, maybe more than code running faster. – Joachim Sauer Mar 29 '23 at 16:27
  • 1
    Don’t believe arbitrary internet pages claiming to have the magic solution that makes things faster. Read *carefully* what you have found and consider that even if the statements on this page were true for Java 6 and Windows XP, it doesn’t mean that this has any relevance for today’s systems. However, to code shown on this page indicates that the author does *not* know, [how to do a correct micro benchmark](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java?noredirect=1&lq=1). – Holger Mar 29 '23 at 16:58

0 Answers0