영어 철저한 검색과 이진 검색을 사용하여 영어 사전을 검색하는 프로그램을 작성하고 있습니다. 나는 각각의 평균을 프린트해야한다. 두 가지 코드는 다음과 같습니다. 나는 문제가 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 초
실패 BINARY 검색 : NaN 초
훨씬 작은 파일을 사용했을 때 나는 이것을 얻었다!
철저한 SEARCH : 0.0 초
BINARY SEARCH : 2.1E-6 초
FAILED 철저한 SEARCH : 1.0E-10 초
BINARY SEARCH 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");
이 문제를 해결하는 방법이 확실하지 않습니다.
queries []가 빈 배열 일 수 있습니까? – FuzzyBunnySlippers
죄송합니다. 컨텍스트가 충분하지 않았습니다. 내 주요 방법으로, 나는 사전 내 사전에있는 모든 단어를 검색하고, 사전에 철저한/이원 검색 방법을 측정 평균이라고 부릅니다. :) – raine
이'/ queries.length'의 값은 무엇입니까? (나는 무엇이 좋은 일인지 의미하지는 않지만 길이는 무엇입니까?) –