简体   繁体   中英

Determining if a program is fast, memory-efficient and does not have large time-complexity

Could someone please help me how I could determine whether my program is memory-efficient, fast and has low time-complexity? For one of my programs, I implemented merge sort and then called some methods here and there but since it has around 100 lines of code, I am skeptical whether it is memory efficient. Thanks guys

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

You can use Profiler. For Java - JProfiler, VisualVM, etc. You can check there all you looking for - how much memory yours algorithms need, the time complexity, and some more stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM