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:
- Recursive: recursive thinking
- Recursion: two conditions that must be met by recursion
- Recursion: recursive determination of string-to-text phenomena
- Recursive: recursive implementation of binary search algorithms
- Recursion: the efficiency of recursion
- Recursion: recursion and loop
- Let's talk about recursion: Is loop and iteration the same thing?
- Recursive computing and iterative computing
- On Recursion: Learn about tail recursion from Fibonacci
- Recursive: Tail recursion and CPS
- Recursion: add more knowledge about Continuation.
- Recursion: Tail recursion in PHP and its optimization
- Recursion: Tail recursion optimization from the perspective of Assembly
This article is available at http://www.nowamagic.net/librarys/veda/detail/2334.