什么时候应该优先考虑递归而不是c ++中的迭代? [英] When should recursion be preferred over iteration in c++?

查看:86
本文介绍了什么时候应该优先考虑递归而不是c ++中的迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我正在阅读文章Iterative vs. Recursive Approaches,我发现迭代过程在这个场景中更可靠。



有人可以说我什么时候我们可以使用迭代而不是递归。



谢谢。



我尝试过:



显示的给定示例显示迭代appraoch的有效结果。我尝试过其他一些示例,但不是它也显示相同的结果。

Hello friends i was reading article "Iterative vs. Recursive Approaches" i found that iterative process is more reliable in this scenarion.

Can some one say me when can we use iteration over recurssion.

Thank you.

What I have tried:

The given example shown is shows efficient results for iterative appraoch.i have tried few of other example but not it also shows the same result.

推荐答案

在大多数情况下,迭代比递归更有效(就执行时间和内存资源而言)。因此,简单的规则是:



如果您可以通过迭代解决问题而不会使代码过于复杂,那么坚持迭代。



迭代通常更有效的原因是,循环结构的成本通常低于函数调用。后者需要存储/加载多个寄存器,包括程序计数器,并且在某些情况下需要执行一些常规工作,例如为异常处理设置上下文。这一切都导致更多的CPU周期和更多的堆栈使用,而不是简单的循环。



另一方面,如果你知道你的递归只是去要进入有限的嵌套级别并且执行时间不是主要问题,您可能更喜欢将代码编写为递归函数,如果这样可以使代码更具可读性和更好的可维护性。



递归通常是更好选择的典型示例包括:解析器中的语法分析,具有大量复杂性的某些数值算法,或者通常在本质上递归并且本质上复杂的算法。



如果有疑问并且性能非常关键,请执行这两种实现并比较它们的运行时行为。
In the majority of cases iteration is by far more efficient (in terms of execution time and memory resources) than recursion. So the simple rule is:

If you can solve a problem by iteration without making the code overly complex, then stick to iteration.

The reason for iteration being generally more efficient is that in general the cost of a loop construct is lower than a function call. The latter requires storing/loading several registers, including the program counter and in some cases doing some routine work like setting up a context for exception processing. That all leads to more CPU-cycles and to more stack usage than a simple loop would take.

On the other hand, if you know that your recursion is only going to go to a limited nesting level and execution time is not a major issue, you might prefer writing your code as a recursive function, if that makes the code more readable and better maintainable.

Typical examples in which recursion is generally the better choice are: Syntax analysis in a parser, certain numerical algorithms with a lot of complexity, or in general algorithms that are recursive in nature and inherently complex.

If in doubt and performance is really critical, do both implementations and compare their run-time behavior.


您找到的文章是什么? :迭代与递归方法 [ ^ ]

这里有一些阅读:

http://www.cs.cornell .edu / info / courses / spring-98 / cs211 / lecturenotes / 07-recursion.pdf [ ^ ]

Fibonacci数字 - 维基百科,免费的百科全书 [ ^ ]

Iteration vs. Recursion in Java [ ^ ]

没有一个答案。

我的经验,递归通常更容易编写,因为它更接近数学定义,迭代通常看起来更复杂但也更有效。



Is the article you found ? :Iterative vs. Recursive Approaches[^]
Here is some reading :
http://www.cs.cornell.edu/info/courses/spring-98/cs211/lecturenotes/07-recursion.pdf[^]
Fibonacci number - Wikipedia, the free encyclopedia[^]
Iteration vs. Recursion in Java[^]
There not a single answer.
My experience, recursion is usually easier to write because it is closer to math definition, iteration generally look more complicated but also more efficient.

Mustafa_ub2016写道:
Mustafa_ub2016 wrote:

有人可以说我什么时候可以使用迭代而不是递归。

Can some one say me when can we use iteration over recursion.

几乎每次都会遇到常见的问题。迭代变得非常复杂只有一些问题。

编写编译器时,通常使用递归,因为它是一个复杂的系统,需要从多个地方调用自己。

Almost every times for usual problems. There is only a few problems where iteration is getting really complicated.
When writing a compiler, recursion is commonly used because it is a complex system that need to call itself from multiple places.


这篇关于什么时候应该优先考虑递归而不是c ++中的迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆