[英]Tail recursion without tail call optimization
Let say we had two versions of a recursive function, with one of them being tail-recursive.假设我们有两个版本的递归 function,其中一个是尾递归的。 Is there any benefit in using the function tail recursive if the language being used does not have tail-call optimization?
如果使用的语言没有尾调用优化,那么使用 function 尾递归有什么好处? From my understanding, without the optimization, each version of a function (tail and non-tail) would use the same number of stack frames (in most cases).
据我了解,如果没有优化,function(尾部和非尾部)的每个版本都将使用相同数量的堆栈帧(在大多数情况下)。
I know that in some cases, like the Fibonacci function for example, using a tail call can be more efficient even without tail-call optimization since it avoids double calls.我知道在某些情况下,例如 Fibonacci function,即使没有尾调用优化,使用尾调用也会更有效,因为它避免了双重调用。 But what if neither version of the function makes double calls?
但是,如果 function 的两个版本都没有进行双重调用怎么办? Would the tail-recursive function still be more efficient?
尾递归 function 还会更有效吗?
The answer to that question is hardware and language implementation dependent.该问题的答案取决于硬件和语言实现。 In most cases, however, I'd have to think that a simple
GOTO
is faster than CALL
- RETURN
instruction pair.然而,在大多数情况下,我不得不认为简单的
GOTO
比CALL
- RETURN
指令对更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.