Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Scanner slower than BufferedReader when reading from input?

I understand what is Scanner good for, and also when to use Scanner and when BufferedReader. I read a different, yet in some therm similar question Scanner vs. BufferedReader

Why is Scanner so slow when I read from the input? I assume it has to do with that there is a small buffer in Scanner, but here I am lost. The original problem is from, Codechef , but I am not interested in that solution.

Here is a code example with a given input: Input:

  • 7 3
  • 1
  • 51
  • 966369
  • 7
  • 9
  • 999996
  • 1

And the code

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

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" "); 
        int numberOfLines = Integer.parseInt(s[0]);
        int divideNumber = Integer.parseInt(s[1]);
        int count = 0;

        for (int i = 0; i < numberOfLines; i++) {
            String number = br.readLine();
            if (number.length() < 11) {
                int num = Integer.parseInt(number);
                if (num % divideNumber == 0) {
                    count++;
                }
            } 
        }
        System.out.println(count);
    }
}

If I read the same code with scanner it is slow.

like image 731
Gábor Csikós Avatar asked Feb 15 '14 12:02

Gábor Csikós


People also ask

Why is BufferedReader faster than scanner in Java?

BufferedReader is a bit faster as compared to scanner because scanner does parsing of input data and BufferedReader simply reads sequence of characters. You might be getting confused with the term synchronous, so to know about it you have to understand the concept of thread. Why do we use BufferedReader in Java?

What is BufferedReader in C++?

A BufferedReader is a simple class meant to efficiently read from the underling stream. Generally, each read request made of a Reader like a FileReader causes a corresponding read request to be made to underlying stream. Each invocation of read() or readLine() could cause bytes to be read from the file,...

How to read data line by line in Java BufferedReader?

Java BufferedReader class is used to read the text from a character-based input stream. It can be used to read data line by line by readLine () method. It makes the performance fast.

How to take other input apart from string in BufferedReader?

As discussed above, BufferedReader doesn’t have the inbuilt methods for taking any other input apart from the string. We can parse the String input into our required type. You can refer to the Implementation below to understand the concept of bufferreader vs scanner class in java.


2 Answers

Beside what has been already said Scanner focus is being a Swiss army knife, it is quite more complex and in simple cases covered by BufferedReader that extra gadgets burden it. It's like sending an aircraft carrier to kill a rat.

like image 178
aalku Avatar answered Sep 28 '22 08:09

aalku


Upper-level classes/methods are generally slower than lower-level classes/methods.
In the same way you could ask why is searching with regular expressions slower than
searching with String.indexOf(). Actually I've seen such questions here on SO.

The more specialized your class/method is, the better it can perform.
It does e.g. just 1 simple thing but does it quickly and efficiently.
More general classes/methods do e.g. 10-20 different things, so they
are more powerful but due to this they are slower.

I am speaking in general here, I haven't compared Scanner and BufferedReader myself.

like image 34
peter.petrov Avatar answered Sep 28 '22 08:09

peter.petrov