斐波那契数列 Java 不一样的实现方法所须要的时间比较

# 首先咱们直接看一个demo以及他的结果函数

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];
        }
    }
}

# 上面代码的计算结果是:spa

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

# 分析:blog

  - 能够看出使用递归计算须要的时间最长递归

  - 咱们不进行理论的分析,再用一个简单的例子去简要说明一下为何递归须要的时间这么长io

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);
        }

    }

}

# 上面代码的输出结果是:class

12586269025
time1 : 48285
count: 25172538049循环

# 分析:
  - 能够看出fun这个函数被调用了不少次,可是这个函数基本上只有一个逻辑,咱们来看看这个比较逻辑调用 25172538049 次会发生些什么吧im

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));
    }
}

# 上面代码的输出结果为:
time1 : 10358demo

# 分析:

  - 上面的代码循环中一共包含两个调用次数较多的逻辑;第一个是比较逻辑,第二个是 自增 逻辑, 能够看出,两个很简单的逻辑调用 25172538049 次就会耗时这么多,那么递归调用加上调用函数的各类开销,一共须要这么长的时间好像也就不足为奇了

相关文章
相关标签/搜索