[js] using cache optimization to recursively calculate fiboracci sequence

First of all, we use the traditional method to write a recursive row number for fiboracci sequence

<script>
    function fib(n){
        return n <=2 ? 1: fib(n - 1) + fib(n - 2);
    }
    var fib = fib(5);
    console.log(fib);
</script>


In this way, we can find out quickly. On the surface, there is nothing wrong with the code. Next, we will count the number of function executions

<script>
    //Used to count the number of execution functions
    var count = 0;
    function fib(n){
        count ++;
        if (n <= 2){
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }
    var fibs = fib(5);  //Test with 5, 10 and 20 respectively
    console.log(fibs);
    console.log("Execution times:" + count);
</script>


From the above we can find that the number of executions increases with the number of data is very large, so what causes the number of executions to be so large?


As shown in the figure above, when we need to find the fifth number, first we need to find the third and fourth numbers;

When calculating the third number, we need to find the first two numbers, that is, the first number and the second number

When calculating the fourth number, you need to repeatedly calculate the third number and the second number. This step is the same as the above step, but you still need to repeat the calculation.

When the amount of data is larger and larger, the amount of data that is repeatedly calculated is larger and larger, so the number of calls is more and more. This is the problem of traditional recursion.


So how can we solve it?

At this time, we need to introduce a concept, cache. When the first calculation is performed, we first store the calculated data in the cache. When the second or higher execution is performed, we first determine whether there is such a number in the cache. If not, we will continue the calculation, and if so, we will take it directly from the cache.


Code implementation:

<script>
    //Define a cache array to store the Fibonacci number that has been calculated
    //To calculate
    //1. First get the number you want from the cache array
    //2. If obtained, directly use
    //3. If it is not obtained, calculate it. After calculation, store the calculation result in cache, and then return the result

    //Statistics of execution times
    var count = 0;
    //Create a closure to prevent external modification of cache array and improve security
    function createFib(){
        //Used to store data
        var cache = [];
        function fib(n){
            count++;
            //If there is data in the cache, return directly
            if (cache[n] != undefined){
                return cache[n];
            }
            //If not in the cache, calculate
            if(n <= 2){
                //Store calculation results in array
                cache[n] = 1;
                return 1;
            }
            var temp = fib(n - 1) + fib(n - 2);
            //Store results in data
            cache[n] = temp;
            return temp;
        }
        return fib;
    }
</script>

We continue to test with numbers 5, 10 and 20


From this we can see that the number of executions is greatly reduced and the efficiency is improved by more than one point.





Posted on Sun, 03 May 2020 08:46:27 -0400 by sridsam