Recursion: Tail recursion in PHP and its optimization

Source: Internet
Author: User
It turns out that tail recursion has no optimization effect in php. The biggest advantage of linear recursive modification to tail recursion is that it reduces the overhead of recursive call stacks. The example of php clearly shows the impact of recursive overhead on the program. However, not all languages support tail recursion. even languages that support tail recursion are generally optimized in the compilation stage, for example, the C language optimizes tail recursion.

Different languages have different support for tail recursion, and compiler optimization is also different. We have read the tail recursion of C language before. how is it in PHP?

PHP does not optimize tail recursion

Let's take a look at the experiment.

 

If XDebug is installed, the following error may occur:

Fatal error: Maximum function nesting level of '100' reached, aborting!

This is a protection mechanism of XDebug, which can be set through the max_nesting_level option. If you open the settings, the program can still run normally.

Even if the code runs normally, as long as we increase the number of parameters, the program will report an error sooner or later:

Fatal error:  Allowed memory size of … bytes exhausted

Why? To put it simply, recursion causes Stack Overflow. Based on the previous ideas, we can try tail recursion to eliminate the impact of recursion on the stack and improve the program efficiency.

 

XDebug also reports an error, and the execution time of the program has not changed significantly.

Fatal error: Maximum function nesting level of '100' reached, aborting!

It turns out that tail recursion has no optimization effect in php.

How PHP eliminates recursion

Let's take a look at the following source code:

 

Now, XDebug no longer issues alert efficiency.

Did you notice the trampoline () function? To put it simply, we use high-order functions to eliminate recursion. For more information about call_user_func_array, see this article: PHP function completion: call_user_func () and call_user_func_array ()

There are also many other methods to avoid stack overflow caused by recursion. for example, in Python, the last call can be eliminated through the decorator and exception, which makes people feel a different kind of holes.

Summary

In C, tail recursive optimization is implemented by the gcc compiler. The biggest advantage of linear recursive modification to tail recursion is that it reduces the overhead of recursive call stacks. The example of php clearly shows the impact of recursive overhead on the program. However, not all languages support tail recursion. even languages that support tail recursion are generally optimized in the compilation stage, for example, the C language optimizes tail recursion. When using tail recursion to optimize code, you must first understand the support of this language for tail recursion.

In PHP, there is no need to use recursion unless it can improve code readability; it is best to consider using technologies such as Tail Call or Trampoline to avoid potential Stack Overflow.

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/2334.

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.