简体   繁体   English

Java运行时间计算

[英]Java Running Time Calculation

I have two different programs I am calculating the running time for using the nano time method. 我有两个不同的程序,正在计算使用纳秒时间方法的运行时间。 It was easy to figure out the first since it was a linear block of code. 由于第一个代码是线性代码块,因此很容易弄清楚。 At the beginning of the code I put this line: 在代码的开头,我把这一行:

long startTime = System.nanoTime();

At the end of the code I put this line: 在代码末尾,我把这一行:

long estimatedTime = System.nanoTime() - startTime;
System.out.println(estimatedTime);

However, my second program has two class files.(see below) Where should I insert the two sections of code to best calculate the running time? 但是,我的第二个程序有两个类文件。(请参见下文)应在哪里插入两段代码以最佳地计算运行时间? Should it be in each class or just one? 是每个班级还是一个班级? Thank you! 谢谢!

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
  1. take start time before while (searchInt != -1) of LinearSearchTest while (searchInt != -1)之前LinearSearchTest

  2. take difference after closing while (searchInt != -1) of LinearSearchTest 采取差异关闭后while (searchInt != -1)LinearSearchTest

Checek where is your static void main function, that will be the one start to run when running your apps. Checek在哪里是您的静态void主要函数,它将在运行您的应用程序时首先运行。 Put your code in that function. 将您的代码放在该函数中。 Each time your run your apps, you will start from that function. 每次运行应用程序时,都将从该功能开始。

for checking the runtime of the whole code, put 'start time' at the start of main method, before 要检查整个代码的运行时,请在主方法的开始处将“开始时间”放在

Scanner input = new Scanner ( System.in );

or if you want to check just the runtime of searching part, put 'start time' just before the while loop. 或者,如果您只想检查搜索零件的运行时间,请将“开始时间”放在while循环之前。

-'estimatedTime' just after the // end while -'estimatedTime'在//结束后

Measuring the time where the program waits for user input is pointless. 测量程序等待用户输入的时间毫无意义。

And timing a linear search in an array of size 10 isn't going to return meaningful values. 并且,在大小为10的数组中进行线性搜索不会返回有意义的值。

What are you trying to achieve by measuring time for a program like this? 您想通过测量此类程序的时间来实现什么?

If you want to compar the linear search algorithm, set up a large number of test runs executed automatically, with a random number to search and time the execution of this batch of executions, then divide by the number of times... 如果要比较线性搜索算法,请设置大量自动执行的测试运行,并使用随机数来搜索和计时这批执行的执行时间,然后除以次数...

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

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