Fibonacci Time columns Java different implementations require relatively

# First, we look at a demo and direct his results

public class QQ {

    public static void main(String[] args) throws ParseException {

        // 1,1,2,3,5,8 ...
        int n = 50;
        Long start = System.currentTimeMillis();
        System.out.println(fun(n));
        System.out.println("time1 : " + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        System.out.println(fun2(n));
        System.out.println("time2 :  " + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        System.out.println(fun3(n));
        System.out.println("time3: " + (System.currentTimeMillis() - start));

    }

    public static long fun(int n) {
        if (n <= 2) {
            return 1L;
        } else {
            return fun(n - 1) + fun(n - 2);
        }

    }

    public static long fun2(int n) {
        if (n <= 2) {
            return 1L;
        } else {
            long a = 1, b = 1, c = 0;
            for (int i = 3; i <= n; i++) {
                c = a + b;
                a = b;
                b = c;
            }
            return c;
        }
    }

    public static long fun3(int n) {
        if (n <= 2) {
            return 1L;
        } else {
            long[] arr = new long[n + 1];
            arr[1] = 1;
            arr[2] = 1;
            for (int i = 3; i <= n; i++) {
                arr[i] = arr[i - 1] + arr[i - 2];
            }
            return arr[n];
        }
    }
}

The above results # code is:

12586269025
time1 : 46059
12586269025
time2 : 0
12586269025
time3: 0

# Analysis:

  - It can be seen using the recursive computation time required for the longest

  - We do not analyze the theory, and then a simple example to briefly explain why the need for such a long time recursion

public class QQ {

    private static long count = 0L;

    public static void main(String[] args) throws ParseException {

        // 1,1,2,3,5,8 ...
        int n = 50;
        Long start = System.currentTimeMillis();
        System.out.println(fun(n));
        System.out.println("time1 : " + (System.currentTimeMillis() - start));
        System.out.println("count: " + count);

    }

    public static long fun(int n) {
        count++;
        if (n <= 2) {
            return 1L;
        } else {
            return fun(n - 1) + fun(n - 2);
        }

    }

}

The output # above code is:

12586269025
time1 : 48285
count: 25172538049

# Analysis:
  - can be seen fun this function is called many times, but this function is basically only a logical, we take a look at this comparison logic to call 25,172,538,049 times what happens.

public class QQ {

    public static void main(String[] args) throws ParseException {

        // 1,1,2,3,5,8 ...
        int n = 50;
        Long start = System.currentTimeMillis();
        for (long i = 0; i < 25172538049L; i++) {
           
        }
        System.out.println("time1 : " + (System.currentTimeMillis() - start));
    }
}

The output of the above code #:
TIME1: 10358

# Analysis:

  - The above code contains a total cycle two more times call logic; the first is the comparison logic, the second is the increment logic, we can see two very simple logic would be time-consuming so call 25172538049 times more, then call the function recursive calls plus a variety of expenses, a total of taking such a long time it seems it is not surprising

 

Guess you like

Origin www.cnblogs.com/lwmp/p/10949713.html