简体   繁体   中英

Receiving NaN when calculating elapsed time in Java

I am writing a program that searches through the english dictionary using both an exhaustive and binary search. I have to print out the averages of each. Here is the code for both. I really don't think the issue is the find and findUsingBinarySearch itself.

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);
}

My output is just:

EXHAUSTIVE SEARCH: NaN seconds

BINARY SEARCH: NaN seconds

FAILED EXHAUSTIVE SEARCH: NaN seconds

FAILED BINARY SEARCH: NaN seconds


When I used a much smaller file, I got this!

EXHAUSTIVE SEARCH: 0.0 seconds

BINARY SEARCH: 2.1E-6 seconds

FAILED EXHAUSTIVE SEARCH: 1.0E-10 seconds

FAILED BINARY SEARCH: 1.4E-6 seconds

Here is how I call the method, using the dictionary as both parameters, as I am trying to test how long it takes the array to binary search itself. I also use a copy of the dictionary with "zzz" appended to each word to facilitate a failed binary and exhaustive search.

    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");

I'm unsure of how to fix this.

If you're trying to convert from milliseconds to microseconds, you should be multiplying, instead of dividing, by 1000.

Your measureAverageExhaustiveSearchTime code:

return (double)((elapsedTime/1000000000.0)/queries.length);

This should be:

return (double)((elapsedTime*1000.0)/queries.length);

Furthermore I would avoid converting the startTime and endTime long datatypes by casting this to a double when solving for elapsedTime . Try to stay in one datatype. Datatype conversions sometimes results in chopped off results, or unexpected flooring of values.

It seems your query array does not contain any elements. So it is empty and its length is zero (0). As an effect you get a division by zero which results in Double.NaN ,

A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero . NaN constants of both float and double type are predefined as Float.NaN and Double.NaN . So, the problem is obviously with (elapsedTime/1000000000.0)/queries.length) and other such statement.

Read this article. The whole article will be extremely useful for you, but this part is especially important:

"NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.

Now, please debug your code and watch your values, especially at your return statement. If you can see something unusual in your arithmetic operations, like 0 divided by 0, then that's the root of the problem.

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