About the use of tail recursion _php example

Source: Internet
Author: User
Tags php example

These days to see several articles on the tail recursion, before the tail recursion is not much concept, so back to study the tail recursion.

The concept of tail recursion
The concept of tail recursion (Tail recursion) is a subset of recursive concepts. For ordinary recursion, the resulting consumption is incalculable because the recursive call stack must be remembered. For example, the first example of the PHP section below uses PHP to write a factorial function, which is due to recursion causing the stack overflow error. The purpose of the tail recursion appears is to eliminate the drawback of recursive stack depletion.


Look at the code level, the tail recursion in fact a word can be said clearly:

The last action of a function is a recursive call

For example, the recursive implementation of PHP for the "Fipona Wedge" sequence:

Copy Code code as follows:

fibonacci.php
<?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 tail recursion, because the last operation of Fibonacci is an addition operation.

Convert to tail recursion:

Copy 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, which adds two accumulators 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 commonly used is functional programming Erlang, where almost all of the functions that appear to be recursive are modified to tail recursion. Here is a look at the performance and application of tail recursion in several different languages.

The tail recursion in PHP
Let's do an experiment.

General recursion:

Copy Code code as follows:

<?php
function factorial ($n)
{
if ($n = = 0) {
return 1;
}
return factorial ($n-1) * $n;
}

Var_dump (factorial (100000000));

Tail Recursion:
Copy Code code as follows:

<?php
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
The tail recursion in PHP is not any optimization effect!

The tail recursion in C

Tail-recursive optimizations in C are done by the GCC compiler. Adding-o2 at the time of GCC compilation will optimize the 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:

Added O2 optimized assembly:

Do not have a big head, I am also the first reading assembly, but this code is very simple, go to the Internet a little search command, roughly can understand:

Copy 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 most important advantage of the general linear recursive modification to tail recursion is that it reduces the overhead of the recursive call stack. It is obvious from the PHP example that recursive overhead affects the program. However, not all languages support tail recursion, even if the language supporting tail recursion is generally optimized for tail recursion in the compile phase, for example, the C language in the example above is optimized for tail recursion. When you use tail recursion to optimize your code, you must first understand the language's support for tail recursion.

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.