Tail recursion optimization in the end is what?

This concept will know when the study data structure, has not been studied.

 

Also a factorial function, the first is usually the most familiar version, which is general recursive version:

 

 

For func (5) of the recursive call is as follows:

 

 

 

 

Then the tail-recursive version:

 

 

 Call graph like this:

 

 It appears that both recursion stack are five Well, what difference does it make?

The biggest difference is: for the first recursive common, each time the function of n * f (n-1) will have to wait f (n-1) call returns, the return to do the multiplication. That is, until f (n-1) before the return, the value of the variable n must be stored on the stack.

For tail-recursive, the function plus the results of a parameter before recording function calculates the (somewhat similar dynamic programming there?). So the current function f (n, 1) finally about to call f (n-1, n) when the variable within the stack (f (n, 1) stack functions) (for example, n and cur_mul) no longer need to be saved a.

The so-called tail recursion optimization does not mean that such an approach is a tail recursion optimization , but rather that our code if you use tail recursion , then the compiler will automatically stack space is no longer needed in the code to optimize away, optimization refers to optimizing compiler that the code tail recursion . Understand that point, if the compiler do not do other things, the performance gap between our tail recursive and general recursive code less, are recursively n layer. Once the compiler recognizes only the tail recursion code will recursive function directly inside the opening in the previous stack (particularly more complicated, and simply understanding the principles of), so that each is used with a recursive stack space, to prevent the possibility of recursion stack layers of high explosive. When finally returned directly to the deepest function result Previous Return to the beginning of the function call. (In fact, it turned into a loop! No longer recursion!)

 

Two affixed know almost answered:

1. tail recursion, one more than the linear recursive parameter, this parameter is a function of the result obtained in the last call;
Therefore, the key point is that each tail recursive calls are included in the collection, to avoid the collection results not only linear recursion Expand memory consumption disadvantages.

What is tail-recursion? - Frankie Young's answer - know almost https://www.zhihu.com/question/20761771/answer/57214778

 
2. As the tail-recursive call occurs at the end of the function, its own stack frame do not have anything that needs to be used, and it can make the next recursive call directly covered by the current stack frame.
Such a result is due to the optimized tail recursion, call / ret actually eliminated (since the current stack frame directly, and does not need to push the stack, the function thereof can generally inline), and the resulting machine code is cycle the same.
What is tail-recursion? - Sun actually answer - know almost https://www.zhihu.com/question/20761771/answer/19144609

Guess you like

Origin www.cnblogs.com/FdWzy/p/12556145.html