The other day, when I was looking at moden C ++, I suddenly thought that iteration and Recursion were the same dongben. In my mind, I thought they were the same, however, in a curious situation, Baidu found that the two were different. I had a program that used recursion and thought, is there any way to optimize it. So let's get to know.
Below are some information found on the Internet
Recursive functions bring about part of runtime Overhead: parameters must be pushed to the stack, memory allocated to local variables, and register values must be saved. When a recursive function is returned every time it is called, all the above operations must be restored to the original format. Therefore, recursive functions are less efficient;
A recursive function can often be rewritten into an iterative form, and iteration is much more efficient than recursion, many problems are described in a recursive way, not because they are suitable for Recursive methods, but because recursive descriptions are more concise and intuitive, the iterative implementation of these problems is often better than recursion. Although the code is slightly less readable, the conciseness of recursion can make up for its runtime overhead when a problem is quite complicated.
Here is an extreme example of the Fibonacci series, which is often described in recursion: WHEN n <= 1, Fibonacci (n) = 1; when n = 2, fibonacci (n) = 1; when n> 2, Fibonacci (n) = fig (n-1) + fig (n-2 );
This description method can easily induce people to use recursive functions to solve the problem:
Long maid (int n)
{
If (n <= 2)
{
Return 1;
}
Return (maid (n-1) + maid (n-2 ));
}
An analysis of the last hop statement will calculate one time for the calculation of Fibonacci (n-1), but one more time for the calculation of the value of the Fibonacci (n-2, this will cause a redundant computing, but the actual situation is much worse than this. Each recursive call will trigger two other recursive calls, either of these two recursive calls triggers two recursive calls, so that the number of redundant computations increases rapidly. For example, when you execute Fibonacci (10), Fibonacci (3) will be executed 21 times, but for recursive computation of Fibonacci (30), Fibonacci (3) it will be calculated more than 0.3 million times (a static variable can be used to calculate the number of executions of the Fibonacci (3 )!! When the more than 0.3 million calculation results are the same, one of them is mandatory and others are a waste.
If iteration is used to solve this problem, the efficiency of the program will be greatly improved, especially when the value of N is quite large:
Long maid (int n)
{
Long result;
Long previus_result;
Long next_older_result;
Result = previus_result = 1;
While (n> 2)
{
N-= 1;
Next_older_result = previus_result;
Previus_result = result;
Result = next_older_result + previus_result;
}
Return result;
}
Summary:Recursion is easy to understand, and the code process is clear. However, each call to a function requires the use of allowed resources. If the number of recursion is large, Stack Overflow may occur due to the need for a stack.
Iteration is repeated in the stack, so this problem does not exist. In comparison, we should use Iteration for efficiency. On the internet, recursion can be converted to iteration, some also say that only tail recursion (from the last expression
Can be converted to iteration. According to my own test, with the help of variables in iteration, it seems that recursive iteration can be completed, but more mental control is needed. So some people say that recursion is difficult for machines and iteration is difficult for people.
Compared with recursion, iterative logic control is complex and requires additional variables to store the intermediate amount. In addition, the Code is not easy to understand. Therefore, we need to know whether to use iteration or recursion.