我尝试测量选择在代码中排序数组的执行时间。选择排序部分正在工作,但基准测试方法"time“甚至没有执行。我的“时间”方法有什么问题,如何让它执行和度量性能时间?
public class Selection_Sort
{
public static void main(String[] args)
{
// define an array
int[] array = {12, 3, 7, 6, 2};
int i;
int j;
int t;
int smallestNumber;
// run an outer loop i from 0 to array.length-1 to repeat the process of selection sort
for(i = 0; i < array.length-1; i++)
{
// smallest number position
smallestNumber = i;
// run an inner loop j for selection sort from i+1 to array.length
for(j = i + 1; j < array.length; j++)
{
// now check if the value at array[j] is smaller than value at array[smallestNumber]
if(array[j] < array[smallestNumber])
{
// if the value is smaller, then store the value of j to smallestNumber
smallestNumber = j;
}
}
// outside the body of inner loop j check if array[i] > array[smallestNumber]. If yes then swap the numbers
if(array[i] > array[smallestNumber])
{
t = array[i];
array[i] = array[smallestNumber];
array[smallestNumber] = t;
}
}
// print the sorted array
System.out.print("Selection Sort:\n");
for(i = 0; i < array.length; i++)
{
System.out.print(array[i]+" ");
}
}
public static void time(String[] args)
{
/* … The code being measured starts … */
long startTime = System.nanoTime();
/* … The code being measured ends … */
long endTime = System.nanoTime();
// get the difference between the two nano time valuess
long timeElapsed = endTime - startTime;
System.out.println("Execution time in nanoseconds: " + timeElapsed);
System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
}
}发布于 2022-09-13 13:23:42
您需要在开始时间和结束时间之间测量代码。一种方法是将要测量的代码部分放在不同的方法中,然后按如下方式调用:
public class Selection_Sort{
public static void sortArray(int[] array)
{
int smallestNumber;
int t;
// run an outer loop i from 0 to array.length-1 to repeat the process of selection sort
for(int i = 0; i < array.length-1; i++)
{
// smallest number position
smallestNumber = i;
// run an inner loop j for selection sort from i+1 to array.length
for(int j = i + 1; j < array.length; j++)
{
// now check if the value at array[j] is smaller than value at array[smallestNumber]
if(array[j] < array[smallestNumber])
{
// if the value is smaller, then store the value of j to smallestNumber
smallestNumber = j;
}
}
// outside the body of inner loop j check if array[i] > array[smallestNumber]. If yes then swap the numbers
if(array[i] > array[smallestNumber])
{
t = array[i];
array[i] = array[smallestNumber];
array[smallestNumber] = t;
}
}
}
public static void main(String[] args)
{
// define an array
int[] array = {12, 3, 7, 6, 2};
/* … The code being measured starts … */
long startTime = System.nanoTime();
sortArray( array);
/* … The code being measured ends … */
long endTime = System.nanoTime();
// get the difference between the two nano time valuess
long timeElapsed = endTime - startTime;
System.out.println("Execution time in nanoseconds: " + timeElapsed);
System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
// print the sorted array
System.out.print("Selection Sort:\n");
for(int i = 0; i < array.length; i++)
{
System.out.print(array[i]+" ");
}
}
}https://stackoverflow.com/questions/73703394
复制相似问题