繁体   English   中英

用netbeans在java中调试代码

[英]debugging a code in java with netbeans

嗨,我想调试这两个代码(递归版本的斐波那契,另一个是迭代版本的斐波那契)。 我想让他们在性能上有所不同。 但我不知道该如何调试这些代码,请帮我谢谢。

斐波那契(递归):

public class Two{

    public static void main(String[] args){
        final Two obj = new Two();
        int sum = 0, i = 1;
        obj.fibonacci(i);
        while(obj.fibonacci(i) < 4000001){
            if(obj.fibonacci(i) % 2 == 0){
                sum += obj.fibonacci(i);
            }
            i++;
        }
        System.out.println(sum);
    }

    public int fibonacci(int n){
        if(n == 0){
            return -1;
        }
        if(n == 1){
            return 1;
        }
        if(n == 2){
            return 2;
        } else{
            return fibonacci(n - 1) + fibonacci(n - 2);

        }
    }
}

斐波那契(迭代):

    int sum = 0,a=1,b=1,c=a+b;
    while (c<4000000){
        sum +=c;
        a=c+b;
        b=a+c;
        c=a+b;
    }
    System.out.println(sum);

在性能测试的代码之前添加以下行:

long start = System.currentTimeMillis();

代码打印出执行所需的时间后:

System.out.println(System.currentTimeMillis() - start);

如果你想知道计算的值改变这些行:

} else {
    return fibonacci(n - 1) + fibonacci(n - 2);
}

List<Integer> tempNumbers = new ArrayList<Integer>();
...

} else {
    int result = fibonacci(n - 1) + fibonacci(n - 2);
    tempNumbers.add(result);
    return result;
}

在要测量的代码之后,打印出列表:

System.out.println(tempNumbers);

暂无
暂无

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

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