关闭扫描仪会不会影响性能 [英] Does closing Scanner affect performance

查看:91
本文介绍了关闭扫描仪会不会影响性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个竞争性问题,有问题的时候,我正在使用扫描仪进行用户输入.
这是2个代码段,一个关闭扫描器,一个不关闭扫描器.

I was solving a competitive problem and in problem, I was taking user input using scanner.
These are 2 code segments, one closing scanner and one without 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();
    }
}

关闭扫描仪

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");
        }           
    }
}

第一个(关闭扫描仪)给我分数14.47
第二个(未关闭扫描仪)显示15.22.
我认为当我使用scanner.close();时,编译器正在释放资源,这就是为什么分数存在差异的原因.

The first one (closing scanner) give me score 14.47,
and second one (not closing scanner) gives 15.22.
I think compiler is freeing the resource when i use scanner.close(); and that is why there is a difference in the scores.

这是分数判定公式.

它的得分为100.假设您提交的解决方案为n 个字符,那么您的分数是(56/n)* 100.

It is assigned a score of 100. Suppose, you submit a solution of n characters, then your score is (56/n)*100.

推荐答案

它的得分为100.假设您提交的解决方案为n 个字符,那么您的得分是(56/n)* 100.

It is assigned a score of 100. Suppose, you submit a solution of n characters, then your score is (56/n)*100.

你是在开玩笑吧?一种解决方案的得分是14.47.这意味着您的源代码是56 / (14.47/100) =~= 387个字符.(=~=表示缺少约等于"符号)

You're kidding right? The score for one solution is 14.47. This means that your source code was 56 / (14.47/100) =~= 387 characters.(=~= for a lack of a "about equals" symbol)

在另一种情况下,您的得分为15.22,这意味着您的源代码长度为56 / (15.22/100) =~= 368个字符.

In the other instance, you had a score of 15.22, which means that your source code was 56 / (15.22/100) =~= 368 characters long.

相差29个字符,这可能是您的源代码行中注明scanner.close();的长​​度(包括前导空格,两个尾随空格和回车/换行符对)

A difference of 29 characters, which is probably the length of your line of source code that says scanner.close(); (including leading spaces, two trailing spaces and a carriage return/line feed pair)

这与代码的性能无关.

这篇关于关闭扫描仪会不会影响性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆