如何使用 Java 测量执行时间
How to measure the execution time with Java
我试图打印下面代码的执行时间,但它总是给我 0 毫秒。网上找了这段代码,想测一下执行情况,所以在最下面加了几行。
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");
}
}
为了让它工作,这一行:
long start = System.currentTimeMillis();
应该在这一行之前:
ob.sort(nums, 0, nums.length-1);
此外,请记住这是测量时间,不是时间复杂度
你的start
和end
是紧挨着的,如果你想计时,开始应该在你计时的操作之前,然后结束。在这种情况下,他们应该将 ob.sort();
夹在中间
附带说明一下,当要排序的集合很大时,计时排序通常会提供更多信息。在小样本量上测试时,许多慢速排序算法的性能似乎与更高效的算法一样好,甚至更好。
我试图打印下面代码的执行时间,但它总是给我 0 毫秒。网上找了这段代码,想测一下执行情况,所以在最下面加了几行。
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");
}
}
为了让它工作,这一行:
long start = System.currentTimeMillis();
应该在这一行之前:
ob.sort(nums, 0, nums.length-1);
此外,请记住这是测量时间,不是时间复杂度
你的start
和end
是紧挨着的,如果你想计时,开始应该在你计时的操作之前,然后结束。在这种情况下,他们应该将 ob.sort();
附带说明一下,当要排序的集合很大时,计时排序通常会提供更多信息。在小样本量上测试时,许多慢速排序算法的性能似乎与更高效的算法一样好,甚至更好。