How slow is a Java Scanner?

There was a topic here that the Java Scanner was very slow, so I measured how slow it was, so I will report it briefly.

Comparison of speeds in Python, Java, C ++

result

number of data=One million Execution time
Java Scanner 714 msec
Own Scanner 24 msec
magnification 29.75

Compared to your own simple Scanner, Java Scanner takes about 30 times longer. It seems better not to use Sanner in competitive programming.

Below is the code used for comparison.

A version that uses the Java Scanner as it is

import java.util.*;
import java.io.*;
import java.util.stream.*;

public class ScannerTest {
    public static void main(String[] args) {
        long t0 = System.currentTimeMillis();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] d = new int[n];
        for (int i = 0; i < n; i++) {
            d[i] = sc.nextInt();
        }
        long t1 = System.currentTimeMillis();
        System.out.println("n = " + n );
        System.out.println((t1-t0) + " msec");
        int sum = IntStream.of(d).sum();
        System.out.println("sum = " + sum);
    }
}

Your own int value reading Scanner version

import java.util.*;
import java.io.*;
import java.util.stream.*;

public class ScannerTest3 {
    static class MyScanner {
        byte[] bytes;
        int pos = 0;
        int len;
        InputStream inputStream;
        MyScanner(InputStream is){
            try{
                inputStream = is;
                bytes = new byte[1024];
                len = Math.max(0,inputStream.read(bytes));
                pos = 0;
                return;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        private byte getNextByte() throws IOException {//throws ArrayIndexOutOfBoundsException
            if( pos >= len ){
                len = Math.max(0,inputStream.read(bytes));
                pos = 0;
            }
            byte result = bytes[pos++];
            return result;
        }
        int nextInt() throws IOException {
            int result = 0;
            byte v;
            while(true){
                v = getNextByte();
                if( v != ' ' && v != '\n' && v != '\r'){
                    break;
                }
            }
            while(true){
                if( v == ' ' || v == '\n' || v == '\r' ){
                    break;
                }
                result *= 10;
                result += (int)v - (int)'0';
                v = getNextByte();
            }
            return result;
        }
    }
    public static void main(String[] args) throws Exception{
        long t0 = System.currentTimeMillis();
        MyScanner sc = new MyScanner(System.in);
        int n = sc.nextInt();
        int[] d = new int[n];
        for (int i = 0; i < n; i++) {
            d[i] = sc.nextInt();
        }
        long t1 = System.currentTimeMillis();
        System.out.println("n = " + n );
        System.out.println((t1-t0) + " msec");
        int sum = IntStream.of(d).sum();
        System.out.println("sum = " + sum);
    }
}

Recommended Posts

How slow is a Java Scanner?
What is a Java collection?
How to make a Java container
Is Java on AWS Lambda slow?
[Java] How to create a folder
Java Calendar is not a singleton.
What is a lambda expression (Java)
How to make a Java array
A story about misunderstanding how to use java scanner (memo)
What is a class in Java language (3 /?)
How to make a Java calendar Summary
[Introduction to Java] How to write a Java program
What is a class in Java language (1 /?)
How to make a Discord bot (Java)
What is a class in Java language (2 /?)
How to print a Java Word document
How to use Java Scanner class (Note)
What is java
java Scanner class
What is Java <>?
What is Java
How to display a web page in Java
How to convert a solidity contract to a Java contract class
How much ternary operator is allowed in Java
How mrbgem is "embedded"
What is a constructor?
How to jump from Eclipse Java to a SQL file
java: How to write a generic type list [Note]
What is a stream
java build a triangle
What is Java Encapsulation?
How to create a data URI (base64) in Java
[Java] How to get a request by HTTP communication
[Java] How to execute tasks on a regular basis
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
How to convert A to a and a to A using AND and OR in Java
What is Java technology?
What is Java API-java
How to convert a file to a byte array in Java
[Java] What is flatMap?
What is a Servlet?
Java11: Run Java code in a single file as is
[Java] What is JavaBeans?
[Java] What is ArrayList?
[Java] How to start a new line with StringBuilder
java Scanner loop input
Docker × Java Building a development environment that is too simple
How to create a lightweight container image for Java apps
How to deploy a simple Java Servlet app on Heroku
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
What is this? Discover how anyone can write a program
How to deploy a kotlin (java) app on AWS fargate
[Java] How to use substring to cut out a character string
Java Language Feature and How it Produced a Subtle Bug
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
[Java] A class is an OS, and an instance is a virtual computer.
[Java] Code that is hard to notice but terribly slow
What is Java Assertion? Summary.