简体   繁体   English

如何用Java测量执行时间

[英]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. 我正在尝试打印以下代码的执行时间,但是它总是给我0毫秒。 I found this code online and wanted to measure the execution, so I added a few lines at the bottom. 我在网上找到了此代码,并想评估执行情况,因此在底部添加了几行。

Java: 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");
}
}

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 另外,请记住,这是在测量时间,而不是时间复杂度

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. 您的startend紧挨着,如果您想给某物计时,则起点应该在您要计时的操作之前,而终点应在之后。 In this case they should sandwich ob.sort(); 在这种情况下,他们应该将ob.sort();夹在中间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. 当在小样本量上进行测试时,许多慢速排序算法的性能似乎可能比高效算法更好,甚至更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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