我的代码测量执行时间的问题

时间:2011-05-20 02:26:22

标签: java sorting time execution-time

所以,我正在尝试测量一些排序方法的执行时间。

这是我的代码:

public static void main(String[] args)
{
    ...

    MeasureExecutionTime(new Runnable() { public void run() { insertionSort(C); } }, "insertionSort()");
}

=====================

private static void MeasureExecutionTime(Runnable r, String s)
{
    startTime = System.nanoTime();
    try
    {
        r.run();
    }
    finally
    {
        endTime = System.nanoTime();
    }
    elapsedTime = endTime - startTime;
    System.out.println(s + " takes " + elapsedTime + " nano-seconds which is " + formatTime(elapsedTime));
}

=====================

public static String formatTime(long nanoSeconds)
{
    long hours, minutes, remainder, totalSecondsNoFraction;
    double totalSeconds, seconds;

    totalSeconds = (double) nanoSeconds / 1000000000.0;
    String s = Double.toString(totalSeconds);
    String [] arr = s.split("\\.");
    totalSecondsNoFraction = Integer.parseInt(arr[0]);
    hours = totalSecondsNoFraction / 3600;
    remainder = totalSecondsNoFraction % 3600;
    minutes = remainder / 60;
    seconds = remainder % 60;
    seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("." + arr[1]));

    StringBuilder result = new StringBuilder(".");
    String sep = "", nextSep = " and ";
    if(seconds > 0)
    {
        if(seconds > 1) result.insert(0, " seconds").insert(0, seconds);
        else result.insert(0, " second").insert(0, seconds);
        sep = nextSep;
        nextSep = ", ";
    }
    if(minutes > 0)
    {
        if(minutes > 1) result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
        else result.insert(0, sep).insert(0, " minute").insert(0, minutes);
        sep = nextSep;
        nextSep = ", ";
    }
    if(hours > 0)
    {
        if(hours > 1) result.insert(0, sep).insert(0, " hours").insert(0, hours);
        else result.insert(0, sep).insert(0, " hour").insert(0, hours);
    }
    return result.toString();
}

我的问题是:

运行此程序后,我输入int[1000000]作为输入,它在大约12-13分钟内执行insertionSort()然后返回:

insertionSort() takes 767186856920 nano-seconds which is 12 minutes and 470.18685692 seconds.

为什么它会给出470秒?我的代码有什么问题?

=========================

修改

seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("." + arr[1]));替换为seconds = seconds + Double.parseDouble("." + arr[1]);后,之前的问题已经消失,但另一个问题出现了:

insertionSort() takes 22864 nano-seconds which is 2.000002864 seconds.

应为0.000022864 seconds.

=========================

EDIT2:

我可能会发现错误。当nanoSeconds很大时,arr[1]就可以了,但当nanoSeconds很小时,arr[1]会转换为指数形式,即14931 nano-seconds => 4.931E-6 seconds.。我该如何解决这个问题?

==========================

EDIT3:

好的,我找到了解决方案:

if(arr[1].contains("E")) seconds = Double.parseDouble("." + arr[1]);
else seconds += Double.parseDouble("." + arr[1]);

4 个答案:

答案 0 :(得分:3)

问题在于:

seconds = Double.parseDouble(Long.toString((long)seconds) + 
                             Double.parseDouble("0." + arr[1]));

seconds 12进入此行,arr[1]"456"。然后

seconds = Double.parseDouble("12" + Double.parseDouble("0.456"));
seconds = Double.parseDouble("12" + 0.456);
seconds = Double.parseDouble("12" + "0.456");
seconds = Double.parseDouble("120.456");
seconds = 120.456.

为什么不这样做:

seconds = seconds + Double.parseDouble("0." + arr[1]);

答案 1 :(得分:0)

我认为你正在连接两个字符串“47”和“0.186 ......”。

答案 2 :(得分:0)

问题在于这一行:

seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("0." + arr[1]));

第二个parseDouble返回“0.18685692”。由于arr[1]已经是小数点右边的字符串,所以只需使用:

seconds = Double.parseDouble(Long.toString((long)seconds) + "." + arr[1]);

答案 3 :(得分:0)

在连接字符串时添加额外的零。为了避免这种问题,最好使用格式化程序来创建字符串。请在此处查看详细信息http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

此外,手动计算正确答案总是有帮助的。这使问题更容易找到。