[英]debugging a code in java with netbeans
Hi I want to debug these two codes (fibonacci with recursive version and the other is fibonacci with iterative version). 嗨,我想调试这两个代码(递归版本的斐波那契,另一个是迭代版本的斐波那契)。 and I want to get the difference between them about performance.
我想让他们在性能上有所不同。 but I don't know that how can I debug these codes ,please help me thanks.
但我不知道该如何调试这些代码,请帮我谢谢。
fibonacci (recursion) : 斐波那契(递归):
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);
}
}
}
Fibonacci (iteration) : 斐波那契(迭代):
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);
Before the code which performance is under test add this line: 在性能测试的代码之前添加以下行:
long start = System.currentTimeMillis();
After the code print out the time needed to perform with: 代码打印出执行所需的时间后:
System.out.println(System.currentTimeMillis() - start);
If you want to know values of the computation change these lines: 如果你想知道计算的值改变这些行:
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
into 成
List<Integer> tempNumbers = new ArrayList<Integer>();
...
} else {
int result = fibonacci(n - 1) + fibonacci(n - 2);
tempNumbers.add(result);
return result;
}
after the code that is being measured print out the list: 在要测量的代码之后,打印出列表:
System.out.println(tempNumbers);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.