kpAtCh
kpAtCh

Reputation: 107

How to measure the execution time with Java

I'm trying to print the execution time of the code below, but it's always giving me 0 milliseconds. I found this code online and wanted to measure the execution, so I added a few lines at the bottom.

Java:


class MergeSort {
void merge(int nums[], int left, int m, int right) {
    int n1 = m - left + 1;
    int n2 = right - m;

    int Left_part_arra[] = new int[n1];
    int Right_part_arra[] = new int[n2];

    for (int i = 0; i < n1; ++i)
    Left_part_arra[i] = nums[left + i];
    for (int j = 0; j < n2; ++j)
    Right_part_arra[j] = nums[m + 1 + j];

    int i = 0,
    j = 0;

    int k = left;
    while (i < n1 && j < n2) {
        if (Left_part_arra[i] <= Right_part_arra[j]) {
            nums[k] = Left_part_arra[i];
            i++;
        } else {
            nums[k] = Right_part_arra[j];
            j++;
        }
        k++;
    }

    while (i < n1) {
        nums[k] = Left_part_arra[i];
        i++;
        k++;
    }

    while (j < n2) {
        nums[k] = Right_part_arra[j];
        j++;
        k++;
    }
}

// merge()
void sort(int nums[], int left, int right) {
    if (left < right) {
        // Find the middle point
        int m = (left + right) / 2;

        // Sort first halve
        sort(nums, left, m);
        // Sort second halve
        sort(nums, m + 1, right);

        // Merge the sorted halves
        merge(nums, left, m, right);
    }
}

// Method to test above
public static void main(String args[]) {
    MergeSort ob = new MergeSort();
    int nums[] = {
        7,
        -5,
        3,
        2,
        1,
        0,
        45
    };
    System.out.println("Original Array:");
    System.out.println(Arrays.toString(nums));
    ob.sort(nums, 0, nums.length - 1);
    System.out.println("Sorted Array:");
    System.out.println(Arrays.toString(nums));

    long start = System.currentTimeMillis();
    long end = System.currentTimeMillis();
    System.out.println("Merge took: " + (end - start) + " milliseconds");
}
}

Upvotes: 1

Views: 4116

Answers (2)

Jonathon K
Jonathon K

Reputation: 339

Your start and end are right next to each other, if you want to time something, start should be before the operation that you are timing, and end after. In this case they should sandwich ob.sort();

As a side note, timing a sort is usually more informative when the collection being sorted is large. Many slow sort algorithms may seem to perform equally well, or even better than, a more efficient algorithm when tested on a small sample size.

Upvotes: 1

Matias Fuentes
Matias Fuentes

Reputation: 461

In order for this to work, this line:

long start = System.currentTimeMillis(); 

should be before this line:

ob.sort(nums, 0, nums.length-1);

Also, keep in mind this is measuring Time and not Time Complexity

Upvotes: 2

Related Questions