我有两个不同的程序我正在计算使用纳米时间方法的运行时间。由于它是一个线性代码块,所以很容易弄清楚第一个。在代码的开头我把这行:
long startTime = System.nanoTime();
在代码的最后,我把这行:
long estimatedTime = System.nanoTime() - startTime;
System.out.println(estimatedTime);
但是,我的第二个程序有两个类文件。(见下文)我应该在哪里插入两段代码来最好地计算运行时间?它应该在每个班级还是只在一个班级?谢谢!
import java.util.Random;
public class LinearArray {
private int[] data; // array of values
private static Random generator = new Random();
// Create an array of a given size and fill it with random numbers
public LinearArray(int size) {
data = new int[size]; // Create space for the array
// fill the array with random ints in the range 10 – 99
for (int i = 0; i < size; i++)
data[i] = 10 + generator.nextInt(90);
} //end of the LinearArray constructor
// Perform a linear search on the data set
public int linearSearch(int searchKey) {
//Search through the array sequentially
for (int index = 0; index < data.length; index++)
if (data[index] == searchKey)
return index; // Return the index of the integer
return -1; // the integer was not found
} // end of the method linearSearch
// a method to output the values in the array
public String toString() {
StringBuilder temporary = new StringBuilder();
// iterate through the array
for (int element : data)
temporary.append(element + " ");
temporary.append("\n");
return temporary.toString();
}
} // end of the class LinearArray
import java.util.Scanner;
public class LinearSearchTest {
public static void main(String args[]) {
// Create Scanner object to input data
Scanner input = new Scanner(System.in);
int searchInt; // the search key
int position; // Location of the search key in the array
// Create and then output the array
LinearArray searchArray = new LinearArray(10);
System.out.println(searchArray); //print the array
// get the input from the user
System.out.print("Please enter an integer value (enter -1 to quit): ");
searchInt = input.nextInt(); // read the first integer value from the user
// repeatedly input an integer; input -1 to terminate the program
while (searchInt != -1) {
// perform the linear search
position = searchArray.linearSearch(searchInt);
if (position == -1) ///the integer was not found
System.out.println("The integer " + searchInt + " was not found. \n");
else // the integer was found
System.out.println("The integer " + searchInt + " was found at position " +
position + ".\n");
// get the input from the user
System.out.print("Please enter an integer value (enter -1 to quit): ");
searchInt = input.nextInt(); // this reads the next input from the user
} // end while
} // end the main method
} // end the class LinearSearchTest
答案 0 :(得分:0)
在while (searchInt != -1)
LinearSearchTest
之前的开始时间
关闭while (searchInt != -1)
LinearSearchTest
后的差异
醇>
答案 1 :(得分:0)
Checek你的静态void main函数在哪里,这将是运行你的应用程序时开始运行的函数。把你的代码放在那个函数中。每次运行应用程序时,您都将从该功能开始。
答案 2 :(得分:0)
用于检查整个代码的运行时间,在“
>之前将”开始时间“放在main方法的开头Scanner input = new Scanner ( System.in );
或者如果您只想检查搜索部分的运行时间,请在while循环之前放置'start time'。
- 在//结束时'估计时间'
答案 3 :(得分:0)
测量程序等待用户输入的时间是没有意义的。
在大小为10的数组中进行线性搜索的时间不会返回有意义的值。
你想通过测量这样一个程序的时间来实现什么目标?
如果你想比较线性搜索算法,设置大量自动执行的测试运行,用随机数搜索并计算执行这批执行的时间,然后除以次数......