简体   繁体   中英

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

  2. take difference after closing while (searchInt != -1) of LinearSearchTest

Checek where is your static void main function, that will be the one start to run when running your apps. 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.

-'estimatedTime' just after the // end while

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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