我正在編寫一個程序,該程序使用窮舉搜索和二分搜索搜索英文詞典。我必須打印每個的平均值。 這是兩者的代碼。我真的不認爲這個問題是find和findUsingBinarySearch本身。在Java中計算經過時間時接收NaN
public static double measureAverageExhaustiveSearchTime(String[] queries, String[] array){
//Measures the average number of microseconds (µs) needed to find each query, using exhaustive search.
long startTime = System.currentTimeMillis();
for(int i = 0; i < queries.length; i++){
find(queries[i], array);
}
long endTime = System.currentTimeMillis();
double elapsedTime = (endTime - startTime);
return (double)((elapsedTime/1000000000.0)/queries.length);
}
public static double measureAverageBinarySearchTime(String[] queries, String[] array){
//Measures the average number of microseconds (µs) needed to find each query, using binary search.
long startTime = System.nanoTime();
for(int i = 0; i < queries.length; i++){
findUsingBinarySearch(queries[i], array);
}
long endTime = System.nanoTime();
double elapsedTime = (endTime - startTime);
return (double)((elapsedTime/1000000000.0)/queries.length);
//(double)(elapsedTime * 1000)/(queries.length);
}
我的輸出就是:
窮舉搜索: 的NaN秒
二進制搜索: 的NaN秒
失敗窮舉搜索: 的NaN秒
失敗的二進制搜索: NaN秒
當我使用一個更小的文件,我得到了這個!
窮舉搜索: 0.0秒
二進制搜索: 2.1E-6秒
FAILED窮舉搜索: 1.0E-10秒
FAILED二進制搜索: 1.4E-6秒
下面是我如何調用方法,使用字典作爲兩個參數,因爲我試圖測試它需要多長時間的數組二進制搜索本身。我還使用附加到每個單詞的「zzz」字典副本來促進失敗的二進制和窮舉搜索。
System.out.println("EXHAUSTIVE SEARCH: ");
System.out.println(measureAverageExhaustiveSearchTime(dictionary, dictionary)+" seconds");
System.out.println("BINARY SEARCH: ");
System.out.println(measureAverageBinarySearchTime(dictionary, dictionary)+" seconds");
System.out.println("FAILED EXHAUSTIVE SEARCH: ");
System.out.println(measureAverageExhaustiveSearchTime(dictionaryzzz, dictionary) + " seconds");
System.out.println("FAILED BINARY SEARCH: ");
System.out.println(measureAverageBinarySearchTime(dictionaryzzz, dictionary)+" seconds");
我不確定如何解決這個問題。
查詢[]是否可能是空數組? – FuzzyBunnySlippers
對不起,我沒有足夠的上下文。在我的主要方法中,我將我的字典中的平均窮舉/二進制搜索方法稱爲搜索字典中的每個單詞。 :) – raine
這個'/ queries.length'的值是什麼? (我不是說它有什麼好處,但是什麼是'長度'?) –