简体   繁体   English

在Java中计算经过时间时接收NaN

[英]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. 我真的不认为问题不是find和findUsingBinarySearch本身。

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 详尽搜索:NaN秒

BINARY SEARCH: NaN seconds 二进制搜索:NaN秒

FAILED EXHAUSTIVE SEARCH: NaN seconds 失败的详尽搜索:NaN秒

FAILED BINARY SEARCH: NaN seconds 失败的二进制搜索:NaN秒


When I used a much smaller file, I got this! 当我使用一个小得多的文件时,我得到了!

EXHAUSTIVE SEARCH: 0.0 seconds 详尽搜索:0.0秒

BINARY SEARCH: 2.1E-6 seconds 二进制搜索:2.1E-6秒

FAILED EXHAUSTIVE SEARCH: 1.0E-10 seconds 失败的搜索:1.0E-10秒

FAILED BINARY SEARCH: 1.4E-6 seconds 失败的二进制搜索:1.4E-6秒

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. 我还使用词典的副本,每个单词后面都附加“ 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");

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. 如果您尝试将毫秒转换为微秒,应乘以1000,而不是除以。

Your measureAverageExhaustiveSearchTime code: 您的measureAverageExhaustiveSearchTime代码:

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 . 此外,在解决elapsedTime时,我可以通过将其转换为double来避免转换startTimeendTime long数据类型。 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). 因此它是空的并且其长度为零(0)。 As an effect you get a division by zero which results in Double.NaN , 结果,您将除以零,结果为Double.NaN

A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero . NaN值用于表示某些无效操作的结果,例如将零除以零 NaN constants of both float and double type are predefined as Float.NaN and Double.NaN . 将float和double类型的NaN常量预定义为Float.NaNDouble.NaN So, the problem is obviously with (elapsedTime/1000000000.0)/queries.length) and other such statement. 因此,问题显然出在(elapsedTime/1000000000.0)/queries.length)和其他此类语句中。

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”代表“不是数字”。 "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. 如果浮点运算具有一些导致该运算产生不确定结果的输入参数,则会生成“ Nan”。 For example, 0.0 divided by 0.0 is arithmetically undefined. 例如,0.0除以0.0在算术上是不确定的。 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. 现在,请调试代码并观察您的值,尤其是在return语句中。 If you can see something unusual in your arithmetic operations, like 0 divided by 0, then that's the root of the problem. 如果您在算术运算中看到异常现象,例如0除以0,那么这就是问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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