These days to see a few articles about the tail recursion, before the end of the recursive not much concept, so back to study the tail recursion.
Concept of tail recursion
The concept of tail recursion (Tail recursion) is a subset of the concept of recursion. For normal recursion, the resulting consumption is incalculable because of the need to remember the recursive call stack. For example, the first example of PHP in the following section uses PHP to write a factorial function, which is due to recursion caused by the stack overflow error. The purpose of tail recursion is to eliminate the drawback of recursive stack depletion.
From the code level, the end of the recursion is actually a word can be said clearly:
The last operation of the function is a recursive call
For example, the recursive implementation of PHP for the "Fibonacci Wedge" sequence:
Copy the Code code as follows:
fibonacci.php
function Fibonacci ($n) {
if ($n < 2) {
return $n;
}
Return Fibonacci ($n-1) + Fibonacci ($n-2);
}
Var_dump (Fibonacci (30));
This is a recursive function, but not a tail recursion, because the last operation of Fibonacci is an addition operation.
Translate to tail recursion:
Copy the Code code as follows:
function Fibonacci2 ($n, $ACC 1, $ACC 2) {
if ($n = = 0) {
return $ACC 1;
}
Return Fibonacci2 ($n-1, $ACC 2, $ACC 1 + $acc 2);
}
Fibonacci2 is a tail recursion, it adds two accumulator acc1 and ACC2, and gives the initial value. Remember: the idea of recursive conversion to tail recursion must be to increase the accumulator and reduce the recursive operation.
The application of tail recursion in different languages is also different. The most common use is functional programming Erlang, where almost all recursive functions are modified to be end-recursive. Here's how tail recursion is performed and applied in several different languages.
Trailing recursion in PHP
Let's do an experiment.
Normal recursion:
Copy the Code code as follows:
function factorial ($n)
{
if ($n = = 0) {
return 1;
}
return factorial ($n-1) * $n;
}
Var_dump (factorial (100000000));
Tail Recursion:
Copy the Code code as follows:
function factorial ($n, $ACC)
{
if ($n = = 0) {
return $ACC;
}
return factorial ($n-1, $ACC * $n);
}
Var_dump (Factorial (100000000, 1));
Experimental results:
It turns out that
Tail recursion in PHP is not any optimization effect!
Tail recursion in C
The tail-recursive optimization in C is done by the GCC compiler. When GCC compiles, plus-o2 will optimize for tail recursion.
We can look directly at the generated assembly code:
(using GDB, Gcc–o2 factorial.c–o factorial; disass factorial)
Assembly not-O2 generated:
The O2 optimized assembly was added:
Don't head big, I also look at the assembly, but this code is very simple, go to the Internet a little search command, roughly can understand:
Copy the Code code as follows:
function factoral (n, sum) {
while (n! = 0) {
sum = n * sum
n = n-1
}
return sum
}
What GCC does is intelligent optimization.
If you are still interested, you can use-O3 to optimize the tail recursion and view the assembly instructions in it.
The optimization of-O3 is to expand the loop directly
Summarize
The advantage of the general linear recursive modification, which is the largest of the tail recursion, is to reduce the overhead of the recursive call stack. The effect of recursive overhead on the program is apparent from the PHP example. However, not all languages support tail recursion, even if the language that supports tail recursion is generally optimized for tail recursion in the compilation phase, such as the optimization of the tail recursion for the C language in the above example. When you use tail recursion to optimize your code, you must first understand the language's support for tail recursion.
http://www.bkjia.com/PHPjc/327067.html www.bkjia.com true http://www.bkjia.com/PHPjc/327067.html techarticle these days to see a few articles about the tail recursion, before the end of the recursive not much concept, so back to study the tail recursion. The concept of tail recursion (Tail recursion) .