int数组与Integer数组的性能比较

10

今天我在Codeforces提交一个解法时,使用了int[]数组。结果我的提交出现了TLE(时间超限)错误。之后我将它改为Integer[]数组,出人意料地获得了AC(通过)。我不知道性能是如何提升的。

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    static class Task {
        public void solve(InputReader in, PrintWriter out) throws Exception {
            int n = in.nextInt();
            Integer[] a = new Integer[n];
            for (int i = 0; i < n; i++) a[i] = in.nextInt();
            Arrays.sort(a);
            long count = 0;
            for (int i = 0; i < n; i++) count += Math.abs(i + 1 - a[i]);
            out.println(count);
        }
    }

    public static void main(String[] args) throws Exception{
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task task = new Task();
        task.solve(in, out);
        out.close();
    }


    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

@sotirios-delimanolis,您能解释一下自动装箱发生的位置吗?如果数组的类型是int[],那么就不应该有自动(取消)装箱。 - Turing85
@NaveenKakani 你唯一改变的是 solve(...) 中数组 a 的类型,从 int[] 变为 Integer[],然后你的程序就运行得更快了? - Turing85
这是指自动拆箱使代码变慢了吗?就是这样吗? - Naveen Kakani
@NaveenKakani 是的,如果Java自动装箱或拆箱变量,就会有运行时开销。 - Turing85
(请注意,您甚至不需要存储不超过一个的输入:只需累加并计数它们。) - greybeard
显示剩余5条评论
1个回答

15
原因很简单:使用Integer的解决方案的时间复杂度更好。
听起来很奇怪,不是吗? Arrays.sort对于原始类型使用双轴快速排序,在最坏情况下的时间复杂度为O(N^2)。您的解决方案无法通过的测试用例是一个特别构造的反向快速排序测试。
然而,对象版本使用归并排序,对于任何可能的输入都以O(N * log N)的时间运行。
请注意,这不是Java语言规范的一部分(它没有说明sort方法应该如何实现),但在大多数真实实现中都是这样的(例如,openjdk-8)。
附注:在竞争性编程中,这种事情或多或少经常发生,因此我建议要么对对象数组进行排序,要么使用集合。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接