开发者

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;
  开发者_StackOverflow      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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜