java - 如何提高操作大型数组的性能?

标签 java arrays performance heap-memory java-io

我现在正在 Hackerrank 上解决一个问题,我相信我的逻辑或多或少是正确的,但是更大的数据集正在降低性能,从而给我一个“错误”的答案。这是问题的链接,您可以查看一下:

https://www.hackerrank.com/challenges/qheap1

我想知道如何提高该脚本的性能以允许更大的数据集。我有预感这与扫描仪有关,但我不知道为什么。

public class Solution {
    private static final int ADD = 1;
    private static final int DELETE = 2;
    private static final int GET = 3;
    private static final int TICK = 1;
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        PrintStream out = System.out;
        int n = in.nextInt();
        int[] heap = new int[n];

        int a = 0;
        while (a < n) {
            a = 0;
            int q = in.nextInt();

            switch(q) {
                case(ADD):
                    int nextAdd = in.nextInt();
                    /*out.println("ADD " + next);*/
                    int b = 0;
                    while (b < n) {
                        /*out.println(heap[b]);*/
                        if (heap[b] == 0) {
                            heap[b] = nextAdd+TICK;
                            /*printArray(heap);*/
                            b = n-1;
                        }
                        b++;
                    }
                    /*printArray(heap);*/
                    break;
                case(DELETE):
                    int c = 0;
                    int nextDelete = in.nextInt();
                    while (c < n) {
                        if (heap[c]-TICK == nextDelete) {
                            heap[c] = 0;
                            c = n-1;
                        }
                        c++;
                    }
                    /*printArray(heap);*/
                    break;
                case(GET):  
                    Arrays.sort(heap);
                    int d = 0;
                    while (d < n) {
                        if (heap[d] != 0) {
                            out.println(heap[d]-TICK);
                            d = n-1;
                        }
                        d++;
                    }
                    /*printArray(heap);*/
                    break;
            }
            a++;
            /*printArray(heap);*/
        }
    }

    public static void printArray(int[] ar) {
        String str = "";
        for (int i : ar) {
            str += i + " ";
        }
        System.out.println(str);
    }
}

最佳答案

看看你的代码,我能发现的唯一直接问题是这一行

out.println(heap[d]-TICK);

没有被注释掉。这可能意味着您的 java 程序(不,它不是脚本,请注意您的措辞!)正在执行大量 IO 操作。与您的程序中进行的任何其他操作相比,这些都非常昂贵

所以,将其注释掉,看看会发生什么。

关于java - 如何提高操作大型数组的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39924579/

相关文章:

arrays - 带有协议(protocol)的 Swift 泛型数组

C 从函数返回 const char 指针或 char 指针

performance - ElasticSearch中_source字段对大型文档(提取的PDF书籍,文档等)有什么影响?

c++ - 在 C++ 中找到两个 vector 之间最相似的值

java - 如何使用java断言视频 Action ?

java - 抽象类中的构造函数

同一台机器上的Java堆空间分配

python - 使用 numpy 拉伸(stretch)、缩放或加倍数组?

MySQL:为什么在优化表后复制到 tmp 表会更快

满足条件的元素的 Java 迭代器