Does the scanner affect performance

I was solving a competitive problem, and in the problem I was accepting user input using a scanner.
These are 2 code segments, one closing scanner and one without a closing scanner.

Closing scanner

import java.util.Scanner;

public class JImSelection {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.valueOf(scanner.nextLine());
        while (n-- > 0) {
            double number = (Math.log(Long.valueOf(scanner.nextLine())) / Math.log(2));
            System.out.println((int) number - number == 0 ? "Yes" : "No");
        }
        scanner.close();
    }
}

Non- closing scanner

import java.util.Scanner;

public class JImSelection {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.valueOf(scanner.nextLine());
        while (n-- > 0) {
            double number = (Math.log(Long.valueOf(scanner.nextLine())) / Math.log(2));
            System.out.println((int) number - number == 0 ? "Yes" : "No");
        }           
    }
}

The first (closing scanner) gives me an estimate 14.47,
and the second (non-closing scanner) gives me 15.22.
I think the compiler frees up the resource when I use it scanner.close();, and that is why the difference in scores.

This is the judge’s assessment formula.

He is assigned a score of 100. Suppose you send a decision to n characters, then your score is (56 / n) * 100.

+4
source share
1

100. , n , (56/n) * 100.

? - 14.47. , 56 / (14.47/100) =~= 387 . (=~= " " )

15.22, , 56 / (15.22/100) =~= 368 .

29 , , , , scanner.close(); ( , /)

.

+4

All Articles