念念不忘,必有回响

Java OJ 快速读入 竞赛用

背景

Java打比赛太吃亏了吧
人家C艹有超快的getchar()
最不济cin的效率也比Java带的Scanner高
还有内存占用方面
竞赛中都不计算Java的内存占用
因为占用太多了( 在空间上卡Java一卡一个准

结论

利用读入二维数组进行测试:

用StreamTokenizer是最快的方法

代码如下:

public class TokenRead {
    public static int[][] bufferedRead() throws IOException {
        StreamTokenizer scanner = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        scanner.nextToken();
        long initTime = System.currentTimeMillis();
        int n = (int) scanner.nval;
        int[][] all = new int[n][];

        for (int i = 0; i < n; i++) {
            scanner.nextToken();
            int count = (int) scanner.nval;
            all[i] = new int[count];
            for (int j = 0; j < count; j++) {
                scanner.nextToken();
                all[i][j] = (int) scanner.nval;
            }
        }

        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

大概是其他读入方法的数量级的差距

对比算法1:

    public static int[][] bufferedRead(String input) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        scanner = new Scanner(new BufferedReader(new InputStreamReader(System.in)));

        int n = scanner.nextInt();
        long initTime = System.currentTimeMillis();
        int[][] all = new int[n][];
        for (int i = 0; i < n; i++) {
            int num = scanner.nextInt();
            all[i] = new int[num];
            for (int j = 0; j < num; j++) {
                int value = scanner.nextInt();
                all[i][j] = value;
            }
        }
        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

对比算法2:

    public static int[][] lineRead(String input) {
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));

        int n = scanner.nextInt();
        long initTime = System.currentTimeMillis();
        scanner.nextLine();

        int[][] all = new int[n][];
        for (int i = 0; i < n; i++) {
            String num = scanner.nextLine();
            String[] nums = num.split(" ");
            int len = Integer.parseInt(nums[0]);
            all[i] = new int[len];
            for (int j = 0; j < len; j++) {
                all[i][j] = Integer.parseInt(nums[j + 1]);
            }
        }

        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

对比算法3:

    public static int[][] forceRead(String input) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long initTime = System.currentTimeMillis();

        int[][] all = new int[n][];
        for (int i = 0; i < n; i++) {
            int num = scanner.nextInt();
            all[i] = new int[num];
            for (int j = 0; j < num; j++) {
                int value = scanner.nextInt();
                all[i][j] = value;
            }
        }
        long nowTime = System.currentTimeMillis();
        System.out.println((nowTime - initTime));
        return all;
    }

posted on 2020-01-31 13:34  licsber  阅读(1500)  评论(0编辑  收藏  举报