[英]Java Running Time Calculation
我有两个不同的程序,正在计算使用纳秒时间方法的运行时间。 由于第一个代码是线性代码块,因此很容易弄清楚。 在代码的开头,我把这一行:
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
在while (searchInt != -1)
之前LinearSearchTest
采取差异关闭后while (searchInt != -1)
的LinearSearchTest
Checek在哪里是您的静态void主要函数,它将在运行您的应用程序时首先运行。 将您的代码放在该函数中。 每次运行应用程序时,都将从该功能开始。
要检查整个代码的运行时,请在主方法的开始处将“开始时间”放在
Scanner input = new Scanner ( System.in );
或者,如果您只想检查搜索零件的运行时间,请将“开始时间”放在while循环之前。
-'estimatedTime'在//结束后
测量程序等待用户输入的时间毫无意义。
并且,在大小为10的数组中进行线性搜索不会返回有意义的值。
您想通过测量此类程序的时间来实现什么?
如果要比较线性搜索算法,请设置大量自动执行的测试运行,并使用随机数来搜索和计时这批执行的执行时间,然后除以次数...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.