Recursion: recursion and loop

Source: Internet
Author: User
We all know that recursion is implemented by calling the function itself. During function calling, address saving and parameter passing are required for each call. this is achieved through a recursive work stack. Recursion uses the system stack to save the local variables in the function to solve the problem. To put it bluntly, the stack processes a bunch of pointers on the stack to objects in the memory. these objects are never released until the last exit of the condition is recursively executed. Therefore, the cost is very high. What is the reason for the so-called slow recursion?

The previous article discussed the efficiency of recursion, but did not go deep into the data structure layer. here I will add some details.

We all know that recursion is implemented by calling the function itself. During function calling, address saving and parameter passing are required for each call. this is achieved through a recursive work stack. Specifically, the content to be saved for each function call includes: local variables, form parameters, call function address, and return value. If recursive call is performed N times, N * local variables, N * form parameters, N * call function address, and N * return values are allocated. This will inevitably affect efficiency.

Recursion uses the system stack to save the local variables in the function to solve the problem. To put it bluntly, the stack processes a bunch of pointers on the stack to objects in the memory. these objects are never released until the last exit of the condition is recursively executed. Therefore, the cost is very high.

Is loop efficiency more efficient than recursion?

Recursion and loop are two different typical solutions. Of course, it doesn't mean that the cycle efficiency must be higher than Recursion. recursion and loop are two different things. recursion carries stack operations, while loops are not necessarily. The two concepts are not one level, make different attempts in different scenarios.

1. recursive algorithms:

  • Advantages: the code is concise, clear, and easy to verify. (If you really understand the algorithm, otherwise you will be dizzy)
  • Disadvantage: its operation requires a large number of function calls. if the number of call layers is deep, additional stack processing is required (or stack overflow may occur ), for example, parameters must be transferred to a stack, which may affect the execution efficiency. However, for some problems, if recursion is not used, it will be an extremely ugly code.

2. loop algorithm:

  • Advantage: Fast speed and simple structure.
  • Disadvantage: all problems cannot be solved. Some problems are suitable for recursive instead of loop. If it is not difficult to use a loop, it is best to use a loop.

3. Summary of recursive and cyclic algorithms:

Recursion is usually a straightforward description of a solution process, so it is also the easiest algorithm to think of and implement. Loop actually has the same characteristics as recursion (that is, repeating tasks), but sometimes the steps to solve the problem are not clearly described using the loop algorithm. From the perspective of algorithm design alone, recursion and loops have no advantages or disadvantages. However, in actual development, recursion often causes performance problems due to the overhead of function calls, especially when the solution scale is uncertain. Loop is more efficient than recursion because there is no function call overhead. Except for a few programming languages that have optimized recursion, most languages are very clumsy in implementing recursive algorithms, which leads to the problem of converting recursive algorithms into cyclic algorithms. Algorithm conversion should be based on a thorough understanding of the solution process, and sometimes it is necessary to find another path.

  • In general, recursive calling can process algorithms, and also solves the need for extra inefficient processing through loops.
  • After the current compiler is optimized, the efficiency of function processing for multiple calls will be very good, and the efficiency may not be lower than the cycle.
  • Recursion and loops are completely interchangeable. If recursive replacement can be easily used without affecting the reading of the program, it is always better to replace it with recursion. (For example, recursive implementation and loop implementation of factorial .)

To convert to non-recursion, perform the following two steps:

  1. Step 1: You can create a stack to save these local variables and replace the system stack;
  2. The second step is to convert recursive calls into loop processing.
So what kind of stack is used recursively?

First, let's take a look at the usage of the system stack and user stack.

  1. The system stack (also called the core stack and kernel stack) is a part of the operating system space in the memory. its main purposes are as follows:
    • Saves the interrupt scene. for nested interruptions, the field information of the interrupted program is pushed into the system stack in sequence. the reverse order of the interrupted response is displayed;
    • Stores the parameters, return values, return points, and local variables of the subprograms called by the operating system.
  2. A user stack is an area of a user's process Space. it is used to save the local variables of parameters, return values, return points, and subprograms (functions) called by user processes.

The recursive program we compile belongs to the user program, so we use the user stack.

Additional reading

The topic list of this article is as follows:

  1. Recursive: recursive thinking
  2. Recursion: two conditions that must be met by recursion
  3. Recursion: recursive determination of string-to-text phenomena
  4. Recursive: recursive implementation of binary search algorithms
  5. Recursion: the efficiency of recursion
  6. Recursion: recursion and loop
  7. Let's talk about recursion: Is loop and iteration the same thing?
  8. Recursive computing and iterative computing
  9. On Recursion: Learn about tail recursion from Fibonacci
  10. Recursive: Tail recursion and CPS
  11. Recursion: add more knowledge about Continuation.
  12. Recursion: Tail recursion in PHP and its optimization
  13. Recursion: Tail recursion optimization from the perspective of Assembly

This article is available at http://www.nowamagic.net/librarys/veda/detail/2322.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.