Data structure and algorithm--the efficiency of recursion and the comparison of recursion and cycle

Source: Internet
Author: User

1. What is the reason for the so-called recursive slowness?

We all know that the implementation of recursion by calling the function itself, the function call, each call to do address preservation, parameter passing, etc., which is achieved through a recursive work stack. In particular, each call to the function itself to save the contents include: local variables, formal parameters, call function address, return value. Then, if you call N times recursively, you will assign n local variables, n parameters, n call function address, n return value. This leads to inefficient efficiency.

2. is recycling efficiency higher than recursion efficiency?

Recursion and circulation are two kinds of typical ways to solve the problem. Of course, it is not that the efficiency of the cycle is higher than recursion, recursion and loop are two different things, recursive with a stack operation, the loop is not necessarily, the two concepts are not a level, different scenarios to do the various attempts.

2.1 Recursive algorithm:

Pros: The code is concise, clear, and easy to verify correctness. (If you really understand the algorithm, you're more dizzy.)

Disadvantage: It requires a larger number of function calls, if the number of calls is deep, you need to add additional stack processing (there may be a stack overflow situation), such as parameter transfer requires a stack and other operations, will have a certain effect on the execution efficiency. However, for some problems, if recursion is not used, it would be extremely ugly code.

2.2 Loop algorithm:

Advantages: Fast speed, simple structure.

Cons: Does not solve all the problems. Some problems are appropriate for using recursion instead of loops. If using loops is not difficult, it is best to use loops.

2.3 Recursive algorithm and loop algorithm summary:

1. Generally recursive calls can be processed by the algorithm, also through the loop to solve the often need for additional inefficient processing.

2. Now the compiler is optimized, the function processing of multiple calls will have very good efficiency optimization, efficiency is not necessarily lower than the loop.

3. Recursion and circulation are completely interchangeable. It is often good to replace recursion if it is convenient to use loop substitution without affecting the reading of the program. (e.g., recursive implementation of factorial and loop implementation.) )

3. What is the stack of recursive use?

First, look at the use of the system stack and the user stack.

3.1 System Stack (also known as the core stack, kernel stack) is an area of memory belonging to the operating system space, the main purpose of which is: (1) Save the interrupt site, for nested interrupts, the field information of the interrupted program is pressed into the system stack, the interrupt returned in reverse order pop-up, (2) Save the operating system subroutine calls between A local variable that returns a value, a return point, and a subroutine (function).

3.2 The user stack is an area in the user process space that holds the parameters, return values, return points, and local variables of subroutines (functions) that are called among the subroutines of the user process.

The recursive program we write belongs to the user program, so we are using the user stack.

Data structure and algorithm--the efficiency of recursion and the comparison of recursion and cycle

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.