簡體   English   中英

Java運行時間計算

[英]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
  1. while (searchInt != -1)之前LinearSearchTest

  2. 采取差異關閉后while (searchInt != -1)LinearSearchTest

Checek在哪里是您的靜態void主要函數,它將在運行您的應用程序時首先運行。 將您的代碼放在該函數中。 每次運行應用程序時,都將從該功能開始。

要檢查整個代碼的運行時,請在主方法的開始處將“開始時間”放在

Scanner input = new Scanner ( System.in );

或者,如果您只想檢查搜索零件的運行時間,請將“開始時間”放在while循環之前。

-'estimatedTime'在//結束后

測量程序等待用戶輸入的時間毫無意義。

並且,在大小為10的數組中進行線性搜索不會返回有意義的值。

您想通過測量此類程序的時間來實現什么?

如果要比較線性搜索算法,請設置大量自動執行的測試運行,並使用隨機數來搜索和計時這批執行的執行時間,然后除以次數...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM