【问题标题】:Determining if a program is fast, memory-efficient and does not have large time-complexity确定程序是否快速、内存效率高且时间复杂度不大
【发布时间】:2018-04-01 21:41:03
【问题描述】:

有人可以帮我确定我的程序是否具有内存效率、速度和时间复杂度低的特点吗?对于我的一个程序,我实现了合并排序,然后在这里和那里调用了一些方法,但由于它有大约 100 行代码,我怀疑它是否具有内存效率。谢谢大家

import java.util.*;

public class Question6 {

    static void mergeSort(Integer arr1[], int o, int k, int x) {

        int num1 = k - o + 1;
        int num2 = x - k;

        int temp1[] = new int[num1]; //creation of two temporary arrays to store in
        int temp2[] = new int[num2];


        for (int i = 0; i < num1; ++i)   //for loops to copy the data to the temporary arrays
            temp1[i] = arr1[o + i];

        for (int j = 0; j < num2; ++j)
            temp2[j] = arr1[k + 1 + j];


        int i = 0, j = 0; //starting position of temporary arrays


        int s = o; //starting position of the merged two temporary arrays
        while (i < num1 && j < num2) {

            if (temp1[i] <= temp2[j]) {
                arr1[s] = temp1[i];
                i++;
            } else {
                arr1[s] = temp2[j];
                j++;
            }
            s++;
        }

        //code to copy elements from temp1
        while (i < num1) {
            arr1[s] = temp1[i];
            i++;
            s++;
        }

        //code to copy elements from temp2
        while (j < num2) {
            arr1[s] = temp2[j];
            j++;
            s++;
        }
    }


    void forSorting(Integer arr2[], Integer t, Integer x) //main method that carries out merge sort
    {
        if (t < x) {
            // Find the middle point
            Integer a = (t + x) / 2;

            // Sort first and second halves
            forSorting(arr2, t, a);
            forSorting(arr2, a + 1, x);

            // Merge the sorted halves
            mergeSort(arr2, t, a, x);
        }
    }

    public static void main(String[] args) {
        Question6 qs = new Question6();
        Scanner sc = new Scanner(System.in);
        Integer[] duplicate = new Integer[10];

        System.out.println("Please input the numbers to be checked for repetition.");

        for (int x = 0; x < 10; x++) {
            duplicate[x] = sc.nextInt(); //filling array
        }

        int length = duplicate.length;
        qs.forSorting(duplicate, 0, length - 1); //calling method forSorting

        System.out.println(Arrays.toString(duplicate)); //displays the array which user fills

        List<Integer> list = Arrays.asList(duplicate); //makes the array duplicate available as a list

        Set<Integer> set = new LinkedHashSet<Integer>(list);

        for ( Integer element : set) {

            if(Collections.frequency(list, element) > 1) {

                System.out.println(" Duplicate: " + element);
            }
        }
    }
}

【问题讨论】:

    标签: java memory-efficient


    【解决方案1】:

    您可以使用 Profiler。对于 Java - JProfiler、VisualVM 等。您可以在那里查看所有您要查找的内容 - 您的算法需要多少内存、时间复杂度以及其他一些内容。

    【讨论】:

    • 我使用 IntelliJ 编程是否重要?还是与 Profiler 无关?
    • 我相信您可以将它与 intellij 一起使用。但我认为这无关紧要。
    • VisualVM 是如何告诉你时间复杂度的?
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多